Last active
March 10, 2016 02:56
-
-
Save jackaperkins/5e598940465dbdee4d54 to your computer and use it in GitHub Desktop.
sets texture on block for later use in unity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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