SceneryProp.cs 2.0 KB

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