Skip to content

Instantly share code, notes, and snippets.

@jackaperkins
Last active March 10, 2016 02:56
Show Gist options
  • Save jackaperkins/5e598940465dbdee4d54 to your computer and use it in GitHub Desktop.
Save jackaperkins/5e598940465dbdee4d54 to your computer and use it in GitHub Desktop.
sets texture on block for later use in unity
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class SetMaterial : MonoBehaviour {
public Texture2D texture;
private MaterialPropertyBlock prop;
void OnRenderObject () {
// materialpropertyblock is what components pass data into, then to the shader
prop = new MaterialPropertyBlock ();
prop.SetTexture("_MainTex", texture);
Renderer r = GetComponent<Renderer> ();
r.SetPropertyBlock (prop);
}
void Awake(){
// make sure we do it first frame too
// since onRenderObject only gets autocalled in editor mode
OnRenderObject ();
}
}
// then, in your shaderlab Properties area, declair your main tex like so:
// // [PER RENDERER DATA] means take texture from materialpropertyblock
// [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment