• Runtimes
  • [Unity] Can I control opacity?

Hello, can I control my spine animation opacity throught unity? I meah, the alpha channel of the color?

If yes, how may I do this throught the editor and throught code?

Thanks.

Related Discussions
...
  • Изменено

I use this to change the color of a group of slots.

public class SpineSlotColorChanger : MonoBehaviour {

public string groupName;
public List<string> slots = new List<string>();
public Color color = new Color(0,0,0,1);

void Start () {
	changeColor();
}

void Update () {
	if (Application.isEditor){
		changeColor();
	}
}

void changeColor(){
	SkeletonAnimation anim = GetComponent<SkeletonAnimation>();
	
	foreach (string slotname in slots){
		foreach (Spine.Slot slot in anim.skeleton.slots){
			if (slotname.Equals(slot.data.name)){
				slot.R = color.r;
				slot.G = color.g;
				slot.B = color.b;
				slot.A = color.a;
			}
		}
	}
}

public List<string> getList(){
	return slots;
}
}

You can add the slot names to the list via inspector and then change the color using the ColorChanger. Of course, yo can do this at runtime too 🙂

Note that if you do an alpha change inside Spine (a fade in/out effect for example) you won't be able to change the color or alpha, because the color change is forced inside the json in "design" time.

If you want to change the color of the whole skeleton, use SkeletonAnimation's skeleton.
The Skeleton class has r, g, b and a fields. you can change at run time.

Note that the Skeleton object is a Spine-C# class and not a Spine-Unity class so it has no knowledge of the UnityEngine.Color type.
So you can/need to change those fields individually.

r, g, b and a are just plain float fields. They normally take values between 0f to 1f.

For example:
To make the skeleton 50% opaque, your code could say:

Spine.Skeleton skeleton = GetComponent<SkeletonAnimation>().skeleton;
skeleton.a = 0.5f;

This doesn't need to be done every Update().

Note that fading out a whole character comprised of many parts using this will still fade out the parts individually. So depending on the style, things may look weird at 50% opacity. Try it out to see what I mean.

5 лет спустя
Pharan написал

If you want to change the color of the whole skeleton, use SkeletonAnimation's skeleton.
The Skeleton class has r, g, b and a fields. you can change at run time.

Note that the Skeleton object is a Spine-C# class and not a Spine-Unity class so it has no knowledge of the UnityEngine.Color type.
So you can/need to change those fields individually.

r, g, b and a are just plain float fields. They normally take values between 0f to 1f.

For example:
To make the skeleton 50% opaque, your code could say:

Spine.Skeleton skeleton = GetComponent<SkeletonAnimation>().skeleton;
skeleton.a = 0.5f;

Is there a way to expose this field in the editor so I can do it as a unity animation instead of in code?

Note that fading out a whole character comprised of many parts using this will still fade out the parts individually. So depending on the style, things may look weird at 50% opacity. Try it out to see what I mean.

Can you give an example of why that would look weird? Is it because some slots might look correct at 25%, but when you fade this way, it'll temporarily increase the opacity?

This thread shows an example of what it looks like if you apply transparency to a character - parts in the back that are usually hidden by overlapping parts in front of it shine through:
Help plz! How do I make my character translucent in Unity?

So in many cases, you don't really want to have this kind of 50% transparency result.
The render-to-texture solution described in the above forum thread would yield the correct result. We will provide an example scene in the future that demonstrates how to properly set things up with render textures, etc.

Harald написал

This thread shows an example of what it looks like if you apply transparency to a character - parts in the back that are usually hidden by overlapping parts in front of it shine through:
Help plz! How do I make my character translucent in Unity?

So in many cases, you don't really want to have this kind of 50% transparency result.
The render-to-texture solution described in the above forum thread would yield the correct result. We will provide an example scene in the future that demonstrates how to properly set things up with render textures, etc.

Unless I'm misunderstanding, that solution is in korean. Is there an english version?

The relevant postings by us are bilingual. Each paragraph is a pair of Korean text above and English text right below, with identical content.

год спустя

Thanks, guys! Super useful!

Thanks for the feedback, glad it helped!

месяц спустя

Hello again! Of course, sorry to bother, but I am having trouble with utilizing the following code:

// set spine opacity
Transform spineTransform = transform.Find("Spine GameObject");
Spine.Skeleton skeleton = spineTransform.GetComponent<SkeletonAnimation>().skeleton;
skeleton.A = 0.5f;

...with the shader Universal Render Pipeline/Spine/Sprite. The opacity appears completely invisible with a value below 0.5f, and only appears slightly darkened with values above 0.5f; I also have the Write to Depth enabled if that changes anything.

I don't think I am doing anything wrong as the code worked fine before changing the shaders and upgrading to URP. It appears to be some sort of internal error, or I am misusing the property.

Please check your Deph Alpha Cutoff value. Most likely you left it at the default 0.5 which explains your problem.

Harald написал

Please check your Deph Alpha Cutoff value. Most likely you left it at the default 0.5 which explains your problem.

Oh! Okay oops, that definitely solved one problem.

The other is still that the asset doesn't appear to be be 50% opaque when its alpha channel is set. Instead alpha/A seems to effect the grayness of an asset rather than its opacity.

Using shader Universal Render Pipeline/Spine/Sprite at an opacity of 1

Using shader Universal Render Pipeline/Spine/Sprite at an opacity of 0.3

A more expected result using shader Spine/Sprite/Unlit at an opacity of 0.3

Awesome! I'll download it and give it a look! As always, you're the best! :grinteeth:

Thanks for your kind words! 🙂 Please let us know it this fixed the problem on your end as well.