AllIn1Shader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. using UnityEditor.SceneManagement;
  6. #endif
  7. [ExecuteInEditMode]
  8. [AddComponentMenu("AllIn1SpriteShader/AddAllIn1Shader")]
  9. public class AllIn1Shader : MonoBehaviour
  10. {
  11. private Material currMaterial, prevMaterial;
  12. private bool matAssigned = false, destroyed = false;
  13. private enum AfterSetAction { Clear, CopyMaterial, Reset};
  14. #if UNITY_EDITOR
  15. private static float timeLastReload = -1f;
  16. private void Start()
  17. {
  18. if(timeLastReload < 0) timeLastReload = Time.time;
  19. }
  20. private void Update()
  21. {
  22. if (matAssigned || Application.isPlaying || !gameObject.activeSelf) return;
  23. SpriteRenderer sr = GetComponent<SpriteRenderer>();
  24. if (sr != null)
  25. {
  26. Renderer r = GetComponent<Renderer>();
  27. if (r.sharedMaterial == null) return;
  28. if (r.sharedMaterial.name.Contains("Default")) MakeNewMaterial();
  29. else matAssigned = true;
  30. }
  31. else
  32. {
  33. Image img = GetComponent<Image>();
  34. if (img != null)
  35. {
  36. if (img.material.name.Contains("Default")) MakeNewMaterial();
  37. else matAssigned = true;
  38. }
  39. }
  40. }
  41. #endif
  42. public void MakeNewMaterial()
  43. {
  44. SetMaterial(AfterSetAction.Clear);
  45. }
  46. public void MakeCopy()
  47. {
  48. SetMaterial(AfterSetAction.CopyMaterial);
  49. }
  50. private void ResetAllProperties()
  51. {
  52. SetMaterial(AfterSetAction.Reset);
  53. }
  54. private void SetMaterial(AfterSetAction action)
  55. {
  56. Shader allIn1Shader = Resources.Load("AllIn1SpriteShader", typeof(Shader)) as Shader;
  57. if (!Application.isPlaying && Application.isEditor && allIn1Shader != null)
  58. {
  59. bool rendererExists = false;
  60. SpriteRenderer sr = GetComponent<SpriteRenderer>();
  61. if (sr != null)
  62. {
  63. rendererExists = true;
  64. prevMaterial = new Material(GetComponent<Renderer>().sharedMaterial);
  65. currMaterial = new Material(allIn1Shader);
  66. GetComponent<Renderer>().sharedMaterial = currMaterial;
  67. GetComponent<Renderer>().sharedMaterial.hideFlags = HideFlags.None;
  68. matAssigned = true;
  69. DoAfterSetAction(action);
  70. }
  71. else
  72. {
  73. Image img = GetComponent<Image>();
  74. if (img != null)
  75. {
  76. rendererExists = true;
  77. prevMaterial = new Material(img.material);
  78. currMaterial = new Material(allIn1Shader);
  79. img.material = currMaterial;
  80. img.material.hideFlags = HideFlags.None;
  81. matAssigned = true;
  82. DoAfterSetAction(action);
  83. }
  84. }
  85. if (!rendererExists)
  86. {
  87. MissingRenderer();
  88. return;
  89. }
  90. else
  91. {
  92. SetSceneDirty();
  93. }
  94. }
  95. else if (allIn1Shader == null)
  96. {
  97. Debug.LogError("Make sure the AllIn1SpriteShader file is inside the Resource folder!");
  98. }
  99. }
  100. private void DoAfterSetAction(AfterSetAction action)
  101. {
  102. switch (action)
  103. {
  104. case AfterSetAction.Clear:
  105. ClearAllKeywords();
  106. break;
  107. case AfterSetAction.CopyMaterial:
  108. currMaterial.CopyPropertiesFromMaterial(prevMaterial);
  109. break;
  110. }
  111. }
  112. public void TryCreateNew()
  113. {
  114. bool rendererExists = false;
  115. SpriteRenderer sr = GetComponent<SpriteRenderer>();
  116. if (sr != null)
  117. {
  118. rendererExists = true;
  119. Renderer r = GetComponent<Renderer>();
  120. if (r != null && r.sharedMaterial != null && r.sharedMaterial.name.Contains("AllIn1"))
  121. {
  122. ResetAllProperties();
  123. ClearAllKeywords();
  124. }
  125. else
  126. {
  127. CleanMaterial();
  128. MakeNewMaterial();
  129. }
  130. }
  131. else
  132. {
  133. Image img = GetComponent<Image>();
  134. if (img != null)
  135. {
  136. rendererExists = true;
  137. if (img.material.name.Contains("AllIn1"))
  138. {
  139. ResetAllProperties();
  140. ClearAllKeywords();
  141. }
  142. else MakeNewMaterial();
  143. }
  144. }
  145. if (!rendererExists)
  146. {
  147. MissingRenderer();
  148. }
  149. SetSceneDirty();
  150. }
  151. public void ClearAllKeywords()
  152. {
  153. SetKeyword("RECTSIZE_ON");
  154. SetKeyword("OFFSETUV_ON");
  155. SetKeyword("CLIPPING_ON");
  156. SetKeyword("POLARUV_ON");
  157. SetKeyword("TWISTUV_ON");
  158. SetKeyword("ROTATEUV_ON");
  159. SetKeyword("FISHEYE_ON");
  160. SetKeyword("PINCH_ON");
  161. SetKeyword("SHAKEUV_ON");
  162. SetKeyword("WAVEUV_ON");
  163. SetKeyword("ROUNDWAVEUV_ON");
  164. SetKeyword("DOODLE_ON");
  165. SetKeyword("ZOOMUV_ON");
  166. SetKeyword("FADE_ON");
  167. SetKeyword("TEXTURESCROLL_ON");
  168. SetKeyword("GLOW_ON");
  169. SetKeyword("OUTBASE_ON");
  170. SetKeyword("ONLYOUTLINE_ON");
  171. SetKeyword("OUTTEX_ON");
  172. SetKeyword("OUTDIST_ON");
  173. SetKeyword("DISTORT_ON");
  174. SetKeyword("WIND_ON");
  175. SetKeyword("GRADIENT_ON");
  176. SetKeyword("COLORSWAP_ON");
  177. SetKeyword("HSV_ON");
  178. SetKeyword("HITEFFECT_ON");
  179. SetKeyword("PIXELATE_ON");
  180. SetKeyword("NEGATIVE_ON");
  181. SetKeyword("COLORRAMP_ON");
  182. SetKeyword("GREYSCALE_ON");
  183. SetKeyword("POSTERIZE_ON");
  184. SetKeyword("BLUR_ON");
  185. SetKeyword("MOTIONBLUR_ON");
  186. SetKeyword("GHOST_ON");
  187. SetKeyword("INNEROUTLINE_ON");
  188. SetKeyword("ONLYINNEROUTLINE_ON");
  189. SetKeyword("HOLOGRAM_ON");
  190. SetKeyword("CHROMABERR_ON");
  191. SetKeyword("GLITCH_ON");
  192. SetKeyword("FLICKER_ON");
  193. SetKeyword("SHADOW_ON");
  194. SetKeyword("ALPHACUTOFF_ON");
  195. SetKeyword("CHANGECOLOR_ON");
  196. SetSceneDirty();
  197. }
  198. private void SetKeyword(string keyword, bool state = false)
  199. {
  200. if (destroyed) return;
  201. if (currMaterial == null)
  202. {
  203. FindCurrMaterial();
  204. if (currMaterial == null)
  205. {
  206. MissingRenderer();
  207. return;
  208. }
  209. }
  210. if (!state) currMaterial.DisableKeyword(keyword);
  211. else currMaterial.EnableKeyword(keyword);
  212. }
  213. private void FindCurrMaterial()
  214. {
  215. SpriteRenderer sr = GetComponent<SpriteRenderer>();
  216. if (sr != null)
  217. {
  218. currMaterial = GetComponent<Renderer>().sharedMaterial;
  219. matAssigned = true;
  220. }
  221. else
  222. {
  223. Image img = GetComponent<Image>();
  224. if (img != null)
  225. {
  226. currMaterial = img.material;
  227. matAssigned = true;
  228. }
  229. }
  230. }
  231. public void CleanMaterial()
  232. {
  233. SpriteRenderer sr = GetComponent<SpriteRenderer>();
  234. if (sr != null)
  235. {
  236. GetComponent<Renderer>().sharedMaterial = new Material(Shader.Find("Sprites/Default"));
  237. matAssigned = false;
  238. }
  239. else
  240. {
  241. Image img = GetComponent<Image>();
  242. if (img != null)
  243. {
  244. img.material = new Material(Shader.Find("Sprites/Default"));
  245. matAssigned = false;
  246. }
  247. }
  248. SetSceneDirty();
  249. }
  250. public void SaveMaterial()
  251. {
  252. #if UNITY_EDITOR
  253. string path = "Assets/AllIn1SpriteShader/Materials/";
  254. if (PlayerPrefs.HasKey("All1ShaderMaterials")) path = PlayerPrefs.GetString("All1ShaderMaterials") + "/";
  255. if (!System.IO.Directory.Exists(path))
  256. {
  257. EditorUtility.DisplayDialog("The desired folder doesn't exist",
  258. "Go to Window -> AllIn1ShaderWindow and set a valid folder", "Ok");
  259. return;
  260. }
  261. path += gameObject.name;
  262. string fullPath = path + ".mat";
  263. if (System.IO.File.Exists(fullPath))
  264. {
  265. SaveMaterialWithOtherName(path);
  266. }
  267. else DoSaving(fullPath);
  268. SetSceneDirty();
  269. #endif
  270. }
  271. private void SaveMaterialWithOtherName(string path, int i = 1)
  272. {
  273. int number = i;
  274. string newPath = path + "_" + number.ToString();
  275. string fullPath = newPath + ".mat";
  276. if (System.IO.File.Exists(fullPath))
  277. {
  278. number++;
  279. SaveMaterialWithOtherName(path, number);
  280. }
  281. else
  282. {
  283. DoSaving(fullPath);
  284. }
  285. }
  286. private void DoSaving(string fileName)
  287. {
  288. #if UNITY_EDITOR
  289. bool rendererExists = false;
  290. SpriteRenderer sr = GetComponent<SpriteRenderer>();
  291. Material matToSave = null;
  292. Material createdMat = null;
  293. if (sr != null)
  294. {
  295. rendererExists = true;
  296. matToSave = GetComponent<Renderer>().sharedMaterial;
  297. }
  298. else
  299. {
  300. Image img = GetComponent<Image>();
  301. if (img != null)
  302. {
  303. rendererExists = true;
  304. matToSave = img.material;
  305. }
  306. }
  307. if (!rendererExists)
  308. {
  309. MissingRenderer();
  310. return;
  311. }
  312. else
  313. {
  314. createdMat = new Material(matToSave);
  315. AssetDatabase.CreateAsset(createdMat, fileName);
  316. Debug.Log(fileName + " has been saved!");
  317. }
  318. if (sr != null)
  319. {
  320. sr.material = createdMat;
  321. }
  322. else
  323. {
  324. Image img = GetComponent<Image>();
  325. img.material = createdMat;
  326. }
  327. #endif
  328. }
  329. private void SetSceneDirty()
  330. {
  331. #if UNITY_EDITOR
  332. if (!Application.isPlaying) EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
  333. #endif
  334. }
  335. private void MissingRenderer()
  336. {
  337. #if UNITY_EDITOR
  338. EditorUtility.DisplayDialog("Missing Renderer", "This GameObject (" +
  339. gameObject.name + ") has no Sprite Renderer or UI Image component. This AllIn1Shader component will be removed.", "Ok");
  340. destroyed = true;
  341. DestroyImmediate(this);
  342. return;
  343. #endif
  344. }
  345. public void ToggleSetAtlasUvs(bool activate)
  346. {
  347. SetAtlasUvs atlasUvs = GetComponent<SetAtlasUvs>();
  348. if (activate)
  349. {
  350. if (atlasUvs == null) atlasUvs = gameObject.AddComponent<SetAtlasUvs>();
  351. atlasUvs.GetAndSetUVs();
  352. SetKeyword("ATLAS_ON", true);
  353. }
  354. else
  355. {
  356. if (atlasUvs != null)
  357. {
  358. atlasUvs.ResetAtlasUvs();
  359. DestroyImmediate(atlasUvs);
  360. }
  361. SetKeyword("ATLAS_ON", false);
  362. }
  363. SetSceneDirty();
  364. }
  365. }