Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Last active January 21, 2018 13:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsubaki/85f82045c5bb4ebfbbeab8f8018e3db5 to your computer and use it in GitHub Desktop.
Save tsubaki/85f82045c5bb4ebfbbeab8f8018e3db5 to your computer and use it in GitHub Desktop.
スプライトアニメーションが使用するテクスチャを上書きする
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);
}
}
@tsubaki
Copy link
Author

tsubaki commented Jan 7, 2018

animation 98

@tsubaki
Copy link
Author

tsubaki commented Jan 9, 2018

animation 102

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment