TC_Image.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using UnityEngine;
  2. using System.Collections;
  3. namespace TerrainComposer2
  4. {
  5. public class TC_Image : MonoBehaviour
  6. {
  7. public RenderTexture rt;
  8. public int referenceCount;
  9. public bool isDestroyed, callDestroy;
  10. void Awake()
  11. {
  12. if (isDestroyed)
  13. {
  14. // LoadRawImage(fullPath);
  15. TC_Settings.instance.imageList.Add(this);
  16. }
  17. if (!callDestroy) { TC.RefreshOutputReferences(TC.allOutput); referenceCount = 0; }
  18. else callDestroy = false;
  19. }
  20. void OnDestroy()
  21. {
  22. if (!callDestroy) TC.RefreshOutputReferences(TC.allOutput);
  23. }
  24. void DestroyMe()
  25. {
  26. TC_Settings settings = TC_Settings.instance;
  27. if (settings == null) return;
  28. if (settings.imageList == null) return;
  29. int index = settings.imageList.IndexOf(this);
  30. if (index != -1) settings.imageList.RemoveAt(index);
  31. TC_Compute.DisposeRenderTexture(ref rt);
  32. #if UNITY_EDITOR
  33. UnityEditor.Undo.DestroyObjectImmediate(gameObject);
  34. #else
  35. Destroy(gameObject);
  36. #endif
  37. }
  38. public void UnregisterReference()
  39. {
  40. --referenceCount;
  41. if (referenceCount > 0) return;
  42. isDestroyed = true;
  43. callDestroy = true;
  44. DestroyMe();
  45. }
  46. }
  47. }