SceneryProp.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. [RequireComponent(typeof(PoolObject))]
  5. public class SceneryProp : MonoBehaviour
  6. {
  7. public float minXScale = 1;
  8. public float maxXScale = 1;
  9. public float minYScale = 1;
  10. public float maxYScale = 1;
  11. public float minRotation = 0;
  12. public float maxRotation = 0;
  13. public float yOffset;
  14. Vector3 _originalScale;
  15. Vector3 _originalPosition;
  16. protected Transform _transform;
  17. private float _minX;
  18. private float _maxX;
  19. public float MinX { get { return _minX; } }
  20. public float MaxX { get { return _maxX; } }
  21. PoolObject _poolObject;
  22. protected virtual void Awake()
  23. {
  24. _transform = transform;
  25. _originalScale = _transform.localScale;
  26. _originalPosition = _transform.localPosition;
  27. _poolObject = GetComponent<PoolObject>();
  28. }
  29. protected virtual void OnSpawnMsg()
  30. {
  31. Vector3 scale = _originalScale;
  32. scale *= 2;
  33. scale.x *= Random.Range(minXScale, maxXScale);
  34. scale.y *= Random.Range(minYScale, maxYScale);
  35. _transform.localScale = scale;
  36. _transform.localRotation = Quaternion.Euler(0, 0, Random.Range(minRotation, maxRotation));
  37. _transform.localPosition = _originalPosition;
  38. AVDebug.Assert(_originalPosition.x == 0 && _originalPosition.y == 0, "Prefab position of Scenery is not at x/y 0,0 - " + name);
  39. RawImage[] textures = GetComponentsInChildren<RawImage>();
  40. AVDebug.Assert(textures != null && textures.Length > 0, "There must be UITextures in ScenerProp "+name);
  41. _minX = float.MaxValue;
  42. _maxX = float.MinValue;
  43. foreach (RawImage t in textures)
  44. {
  45. var tex= t.mainTexture;
  46. t.material = MenuManager._instance.mat;
  47. // t.shader = Shader.Find("Unlit/Transparent Colored");
  48. t.texture = tex;
  49. // Vector3[] corners = NGUIMath.CalculateWidgetCorners(t);
  50. //foreach (Vector3 v in corners)
  51. //{
  52. // if (v.x < _minX)
  53. // {
  54. // _minX = v.x;
  55. // }
  56. // if (v.x > _maxX)
  57. // {
  58. // _maxX = v.x;
  59. // }
  60. //}
  61. }
  62. _transform.localPosition += new Vector3(0, yOffset);
  63. }
  64. public void Despawn()
  65. {
  66. _poolObject.Despawn();
  67. }
  68. }