Last active
January 21, 2018 13:58
-
-
Save tsubaki/85f82045c5bb4ebfbbeab8f8018e3db5 to your computer and use it in GitHub Desktop.
スプライトアニメーションが使用するテクスチャを上書きする
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 System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
[RequireComponent(typeof(SpriteRenderer))] | |
public class OverrideSprite : MonoBehaviour | |
{ | |
private SpriteRenderer sr; | |
private static int idMainTex = Shader.PropertyToID("_MainTex"); | |
private MaterialPropertyBlock block; | |
[SerializeField] | |
private Texture texture = null; | |
public Texture overrideTexture{ | |
get{ return texture; } | |
set{ | |
texture = value; | |
if (block == null) { | |
Init (); | |
} | |
block.SetTexture (idMainTex, texture); | |
} | |
} | |
void Awake() | |
{ | |
Init (); | |
overrideTexture = texture; | |
} | |
void LateUpdate() | |
{ | |
sr.SetPropertyBlock (block); | |
} | |
void OnValidate() | |
{ | |
overrideTexture = texture; | |
} | |
void Init() | |
{ | |
block = new MaterialPropertyBlock (); | |
sr = GetComponent<SpriteRenderer> (); | |
sr.GetPropertyBlock (block); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment