Mostly just a learning experience, but the following script came with an asset I bought and it is adding a GC allocation. I would like to see how to eliminate it. My thought was to get the renderer in Awake and then replace the GetComponent calls in update like
sunrenderer.material.SetTextureOffset (“_BumpMap”, Vector2(offsetX,offsetY));
instead of
GetComponent.().material.SetTextureOffset (“_BumpMap”, Vector2(offsetX,offsetY));
but that gave an error. Unknown identifier: ‘sunrenderer’ in each of the update lines that used it.
var scrollSpeedX = 0.015;
var scrollSpeedY = 0.015;
var scrollSpeedXMaterial2 = 0.015;
var scrollSpeedYMaterial2 = 0.015;
function Awake ()
{
//sunrenderer = GetComponent.<Renderer>();
}
function Update () {
var offsetX = Time.time * scrollSpeedX%1;
var offsetY = Time.time * scrollSpeedY%1;
var offset2X = Time.time * scrollSpeedXMaterial2%1;
var offset2Y = Time.time * scrollSpeedYMaterial2%1;
GetComponent.<Renderer>().material.SetTextureOffset ("_BumpMap", Vector2(offsetX,offsetY));
GetComponent.<Renderer>().material.SetTextureOffset ("_MainTex", Vector2(offsetX,offsetY));
if(GetComponent.<Renderer>().materials.Length>1){
GetComponent.<Renderer>().materials[1].SetTextureOffset ("_MainTex", Vector2(offset2X,offset2Y));
GetComponent.<Renderer>().materials[1].SetTextureOffset ("_BumpMap", Vector2(offset2X,offset2Y));
}
}
GetComponent doesn’t cause any allocations. Use the profiler, and turn on deep profile. Also, setting Renderer.materials like that doesn’t work; you need to assign the materials to an array, change the appropriate material in the array, then assign it back. As for the error, you have to declare variables before you can use them.
–Eric
It is Renderer.get_materials () with 3 calls causing the GC allocations.
The script works in game, and creates an animated texture ( of the sun) which looks good.
Solved. So the 3 calls were coming from the IF statement and the two lines after it where it was looking for the 2nd item in the array. The two GetComponent statements before the IF statement were not causing GC allocations. But I honestly am not sure why he was even making it so complicated when in fact, it could be done very easily without any of that mess just by assigning the two materials as public variables to begin with. The materials were provided with the package, so the script could have just assigned them to begin with. My updated code without any GC allocations which is basically 101 (although to be fair I butchered the JS for awhile).
var scrollSpeedX = 0.015;
var scrollSpeedY = 0.015;
var scrollSpeedXMaterial2 = 0.015;
var scrollSpeedYMaterial2 = 0.015;
public var materialOne : Material;
public var materialTwo : Material;
function Update ()
{
var offsetX = Time.time * scrollSpeedX%1;
var offsetY = Time.time * scrollSpeedY%1;
var offset2X = Time.time * scrollSpeedXMaterial2%1;
var offset2Y = Time.time * scrollSpeedYMaterial2%1;
materialOne.SetTextureOffset ("_BumpMap", Vector2(offsetX,offsetY));
materialOne.SetTextureOffset ("_MainTex", Vector2(offsetX,offsetY));
materialTwo.SetTextureOffset ("_MainTex", Vector2(offset2X,offset2Y));
materialTwo.SetTextureOffset ("_BumpMap", Vector2(offset2X,offset2Y));
}
And now I have my beautiful sun with 0 GC allocations.