Skip to content

Instantly share code, notes, and snippets.

@DenchiSoft
Last active May 23, 2019 19:52
Show Gist options
  • Save DenchiSoft/4adedeaa6e32d0b37c5dc98135bd7a90 to your computer and use it in GitHub Desktop.
Save DenchiSoft/4adedeaa6e32d0b37c5dc98135bd7a90 to your computer and use it in GitHub Desktop.
using System.Linq;
using Live2D.Cubism.Core;
using Live2D.Cubism.Rendering;
using UnityEditor;
using UnityEngine;
/// <summary>
/// Replace active Live2D model texture temporarily
/// at runtime or permanently in editor.
/// </summary>
public class CubismTextureReplacer : MonoBehaviour
{
public CubismModel model;
public Texture2D newTexture;
#if UNITY_EDITOR
[CustomEditor(typeof(CubismTextureReplacer))]
public class LauncherEditor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();
var textureReplacer = target as CubismTextureReplacer;
if (textureReplacer != null && GUILayout.Button("Replace model texture"))
{
textureReplacer.Reload();
}
}
}
#endif
public void Reload()
{
if (model != null)
{
if (newTexture != null) {
foreach (var drawable in model.Drawables.ToList())
{
CubismRenderer r = drawable.gameObject.GetComponent<CubismRenderer>();
if (r != null) {
r.MainTexture = newTexture;
}
}
Debug.Log("Replaced texture for model \"" + model.name + "\" (" + model.Drawables.Length + " drawables).");
}
else
{
Debug.Log("Set replacement texture first.");
}
}
else
{
Debug.Log("Set model first.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment