I see how to set a particle's color based on a gradient (e.g. this & this) but I can't find anything on how to get the color.
I have a particle system with the start color set to random with a gradient. I've tried...
Color myColor = myParticleSystem.main.startColor.color
... but it always returns black, regardless of the gradient colors.
I don't see anything in the docs or forums about how to actually get the color that was randomly chosen.
Using Unity 2017.3. Thanks.
At the moment, we can't read the MinMaxCurve from scripts, as per here: https://blogs.unity3d.com/2016/04/20/particle-system-modules-faq/ (scroll down to the Easing the Pain section).
However, your code returns not a MinMaxCurve, but the Start Color of type Color that you can set via inspector or via script.
For example, if you create a Particle System game object in a scene, and you attach to it this simple script:
using UnityEngine;
public class ParticlesTest : MonoBehaviour {
ParticleSystem myParticleSystem;
public Color myColor;
private void Awake() {
myParticleSystem = GetComponent<ParticleSystem>();
}
private void Update() {
myColor = myParticleSystem.main.startColor.color;
}
}
you can see that myColor changes when you change the Start Color value of the Particle System in Play mode.
Related
I'm very new on shader programming and would be interested in modify urp lit shader to support setting properties in objects with MaterialPropertyBlock on runtime.
The objective is to have several objects with the same material 'tinted' with a different color each one.
Thanks!
I don't know shader programming but if you want several objects sharing a material to be tinted with different colors a script like this might work :
public class Tint : MonoBehaviour
{
public Color TintColor;
[Range(0,1)]
public float TintIntensity = 0.5f;
private void OnEnable()
{
Material material = GetComponent<MeshRenderer>().material;
material.color = Color.Lerp(material.color,TintColor,TintIntensity);
}
}
For example I want to change the mouth blend shape value from 0 to 23 randomly to simulate talking when I'm showing text. It's no really the lips will move like the text it's just to simulate talking when the text show so I want to make something more or less randomly values between 0 and 23.
I tried this but it's not working it does nothing. It's not changing the mouse blendshape value at all.
The scripts is attached to the object with the blendshapes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlendShapesController : MonoBehaviour
{
private SkinnedMeshRenderer bodySkinnedMeshRenderer;
// Start is called before the first frame update
void Start()
{
bodySkinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
}
// Update is called once per frame
void Update()
{
int rand = Random.Range(0, 23);
bodySkinnedMeshRenderer.SetBlendShapeWeight(0, rand);
}
}
Solution : Since the object with the SkinnedMeshRenderer is a child and the parent have a Animator component I had to change the Animator component settings on the parent object to Animate Physics and Always Animate.
I'm using by index not by name but it's working so I will consider it for the meanwhile as a solution since it's working.
I tried to change the color via script on runtime of the cursor and it worked to 75%:
Mesh_top is the only part that does not change the color and I dont know why.
All 4 parts use the same material, named "cursormaterial".
What I tried:
Changing the color by referencing to cursormaterial
Changing the color by getting the component SkinnedMeshRenderer
Trying to use ProptertyBlock
In all three cases I got the same result. The only thing that works is before hitting play I can change the color, this will change the color of the whole cursor. Changing it on runtime works only for 3 of 4 parts...ยด
--Edit--
public SkinnedMeshRenderer cursorRendererOne, cursorRendererTwo, cursorRendererThree, cursorRendererFour;
private MaterialPropertyBlock _propBlock;
public Material material;
void Start()
{
_propBlock = new MaterialPropertyBlock();
}
public void OnInputDown(InputEventData eventData)
{
if (!isActivated)
{
//#1
material.color = Color.blue;
//#2
cursorRendererOne.sharedMaterial.color = Color.blue;
//#3
cursorRendererOne.GetPropertyBlock(_propBlock);
_propBlock.SetColor("_Color", Color.blue);
cursorRendererOne.SetPropertyBlock(_propBlock);
cursorRendererTwo.SetPropertyBlock(_propBlock);
cursorRendererThree.SetPropertyBlock(_propBlock);
cursorRendererFour.SetPropertyBlock(_propBlock);
isActivated = true;
}
Here u see the changed material, but the mesh_top looks but different:
This is a "Bug" (maybe an intended one?).
Open the Animation window (CTRL + 6)
And in the hierachy select the CursorVisual
If you now go to the animation called CursorWaitingAnim you can see there is a keyframe for the top_mesh color.
This single keyframe causes that the color of that tile can not be changed on runtime. The reason is that the animator runs after OnInputDown so it reverts the changes for any keyframed property.
So if you don't need the Waiting animation simply remove that keyframe.
=> you can manipulate the color at runtime again!
Alternatively you could replace it by a one that instead of fixing the color simply disables the SkinnedMeshRenderer instead which basically has more or less the same effect but doesn't screw the colors:
Ok i know im so new to code its insane but i have made some progress in my books with my little game..yes it took me a month to figure out to move a character back and forth using arrow key but i did it nonetheless..So now im really stuck
how do i make my player change color (for 3 seconds) when hit from an object from above.
its 2D Csharp unity...object is falling from Y axis and my player is X axis...cause if i can do that ...then over time i can apply animation on the player when an object hits it.
Here's a script i quickly cooked up for you.
You should do more research, read tutorials, and watch more tutorials.
http://docs.unity3d.com/ScriptReference/Collider2D.OnCollisionEnter2D.html
http://docs.unity3d.com/ScriptReference/Color.Lerp.html
using UnityEngine;
using System.Collections;
public class changeColorOnHit : MonoBehaviour {
SpriteRenderer sr;
void Start(){
sr = GetComponent<SpriteRenderer>();
}
void OnCollisionEnter2D(Collision2D collision){//whenever we hit something
sr.color = new Color(2,0,0);//set this object's red color to 200 percent
}
void Update(){
//linear interpolation brings two values closer together proportional to a given third value(time)
sr.color = Color.Lerp (sr.color,Color.white,Time.deltaTime/1.5f);//slowly linear interpolate. takes about 3 seconds to return to white
}
}
I have a main_mesh that has 10 submeshes, I wonder how I can change the color of of these submeshes to a different color (e.g submesh1 will have a red color, submesh2 will have a blue color,...etc). Any advise please?
UPDATE:
This is how I'm getting my mesh which has 10 submeshes:
SkinnedMeshRenderer smr = gameobject1.GetComponent<SkinnedMeshRenderer>();
Mesh main_mesh = smr.sharedMesh;
SkinnedMeshRenderer smr = gameobject1.GetComponent<SkinnedMeshRenderer>();
Mesh main_mesh = smr.sharedMesh;
smr.materials[0].color = Color.red; // Change submesh1 to red color
smr.materials[1].color = Color.blue; // Change submesh2 to blue color
...
smr.materials[n].color = ... // Change submesh n to whatever color
Since you've added the tag Unityscript i'll assume that you want to be able to change submeshes inside a script.
Assignation as parameter
The first solution would be to have add a public parameter to you script that would be an array of Mesh. Then assign manually each submesh to the array through the inspector. Now you can access the material of each mesh and change it's color.
public class MyScript : MonoBehaviour {
public Mesh[] submeshes;
// Update is called once per frame
void Update () {
for (int i = 0; submeshes[i]; i++) {
// Return the first material of the mesh renderer, use .materials if multiple Material are applied
submeshes[i].renderer.material.color = Color.red;
}
}
}
Note that I used Mesh as type for my array, but you could directly use Material if you only want to change the color.
Also, if submeshes have the exact same material, It'll change the color for all submeshes, not just one. You need to have one material per mesh.
While this solution is not viable if your number of submeshes change dynamically, this solution is pretty simple and straighforwarded.
Use children
Instead of assigning every submeshes manually, you can dynamically change the color by accessing children
public class MyScript : MonoBehaviour {
public Mesh myObject;
// Update is called once per frame
void Update () {
Material[] array = myObject.GetComponentsInChildren<Material>();
for (int i = 0; array[i]; i++) {
array[i].color = Color.red;
}
}
}
This solution allows you to have N material assigned to your submeshes.
Here is the documentation for GetComponentsInChildren
Edit
Short answer If you want a specific answer to your case, it depends on the materials and shaders assigned to your skinned Mesh Renderer because they can override or alter your childrens' materials. If not, the below code should work.
SkinnedMeshRenderer smr = gameobject1.GetComponent<SkinnedMeshRenderer>();
Mesh main_mesh = smr.sharedMesh;
Mesh[] submeshes = main_mesh.GetComponentsInChildren<Mesh>();
for (int i = 0; submeshes[i]; i++) {
// If your submesh already have a material, remove the first line below !
submeshes[i].renderer.material = new Material(Shader.Find("Diffuse"));
submeshes[i].renderer.material.color = Color.red;
}
This solution create a new material for each submesh, which is quite brutal.
In the inspector, you should assign one material to each submeshes and then use always the same material with different colors.
In case it doesn't work
When you want to change the color of one specific mesh, this mesh needs to have his own material. The color of the mesh will depends on this materials and it's properties (shaders, textures, colors).
With a Skinned Mesh Renderer, you generally use Diffuse Material with textures to apply colors to one complex mesh. In some case, this mesh apply the color to it's childrens.
When using a Skinned Mesh Renderer, you usually use a UV texture. This particular texture is created based on your 3D object and is used to apply multiple color on it (sometimes also it's childrens). Here is a simple example of UV texture and here is a more complex example.
Note that, as a mesh Renderer, a skinned mesh renderer can have multiple materials which make the situation more complex but the principle remains the same.
SkinnedMeshRenderer smr = gameobject1.GetComponent<SkinnedMeshRenderer>();
Mesh main_mesh = smr.sharedMesh;
With your code if main_mesh use a UV texture, you have two solutions
Remove the texture then apply a color to it's children
Create a specific UV texture which apply colors as you want.