-
Notifications
You must be signed in to change notification settings - Fork 0
/
TexturePlaceholderSetter.cs
49 lines (44 loc) · 1.24 KB
/
TexturePlaceholderSetter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class TexturePlaceholderSetter : MonoBehaviour
{
public Material targetMaterial;
public Texture mainTexture;
#if UNITY_EDITOR
[InitializeOnLoadMethod]
private static void Initialize()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;
}
private static void OnPlayModeStateChanged(PlayModeStateChange state)
{
if (state == PlayModeStateChange.ExitingPlayMode)
{
SetMainTextures();
}
}
#endif
private static void SetMainTextures()
{
foreach (TexturePlaceholderSetter textureSetter in FindObjectsOfType<TexturePlaceholderSetter>())
{
SetMainTexture(textureSetter);
}
}
private static void SetMainTexture(TexturePlaceholderSetter textureSetter)
{
if (textureSetter.targetMaterial && textureSetter.mainTexture)
{
textureSetter.targetMaterial.SetTexture("_MainTex", textureSetter.mainTexture);
#if UNITY_EDITOR
EditorUtility.SetDirty(textureSetter.targetMaterial);
#endif
}
else
{
Debug.LogWarning("Target Material or Main Texture is not assigned.");
}
}
}