To start with, I don’t think I understand the MaterialPropertyBlock concept entirely, however I used it and I’m now stuck. The scenario is that I instantiate gameobjects, which use the same material, although they have different value for the color property (different color in short). To achieve this I implemented the MaterialPropertyBlock method and indeed the objects are instantiated with different colors. Next (in a second pass) I need to refer to one of these gameobject’s color, but if I use oneOfTheseGOs.renderer.material.color, it will return the original color of the material. Which makes sense after all, but how, then, do I get the value for color which was passed with the MaterialPropertyBlock when the GO instantiated? Do I have to store it somehow, but in a way that when I refer to a GO, I can get it from there (like with the renderer.material.color method). I have tried different approaches to no avail. Also by googling cannot find enough working examples, which use MaterialPropertyBlock (and particularly GetPropertyBlock).
Thanks for reading!
I was about to post some code, in order to make things a bit clearer (and get some suggestions), but I managed to figure out how to achieve my goal. In this case, I will post the code with the solution, but having talked about rarity of examples, I will also try to provide a minimal but comprehensive (untested) picture of my procedure.
#pragma strict
private var materialProperty : MaterialPropertyBlock;
private var tagID : int;
var prefab : GameObject;
function Start() {
var position = Vector3(0, 0, 0);
var oneGO : GameObject = Instantiate(prefab, position, Quaternion.identity);
var color : Color = Color.blue;
materialProperty = new MaterialPropertyBlock();
tagID = Shader.PropertyToID("_Color");
materialProperty.Clear();
materialProperty.AddVector(tagID, color);
departmenoid.renderer.SetPropertyBlock(materialProperty);
position = Vector3(1, 1, 1);
var anotherGO : GameObject = Instantiate(prefab, position, Quaternion.identity);
var color = Color.yellow;
materialProperty.Clear();
materialProperty.AddVector(tagID, color);
departmenoid.renderer.SetPropertyBlock(materialProperty);
//In a second pass
var objectHasColor : Color;
oneGO.renderer.GetPropertyBlock(materialProperty);
var colorVec : Vector4 = materialProperty.GetVector(tagID);
objectHasColor = colorVec;
//this will return Blue; and if we do
anotherGO.renderer.GetPropertyBlock(materialProperty);
colorVec = materialProperty.GetVector(tagID);
objectHasColor = colorVec;
//this will return Yellow.
}
My confusion regarded my lack of deeper understanding of using voids, but better later than ever…