using UnityEngine; using AX.MessageSystem; using System; using System.Collections; using System.Collections.Generic; using UnityEngine.EventSystems; //Author:ZCG //CreatTime:12/2/2017 /// ///改变物体Transform /// public class BaseTransformChange : MonoBehaviour { [SerializeField] protected Transform obj;//需要控制的物体 [SerializeField] protected TransformType transformType;//类型:位置 旋转 缩放 [SerializeField] protected ValueChange valueChanage;//数值变化方式 乒乓 重复 //[SerializeField] //protected Axis axis = Axis.x;//轴 [SerializeField] protected float min = 0;//开始参数 [SerializeField] protected float max = 1;//结束参数 [SerializeField] [Tooltip("Repat/PingPong变化速度")] [Range(0.1f,10f)] protected float speed = 1;//速度 [SerializeField] [Tooltip("Random改变的频率")] [Range(0f,100f)] private float changeTime = 1;//随机数的改变频率 [SerializeField] protected bool x, y, z;//需要控制的轴 protected virtual void Awake() { MessageDispatcher.AddListener("EQUALS_COMMAND", PressureUp); MessageDispatcher.AddListener("MINUS_COMMAND", PressureDown); MessageDispatcher.AddListener("U_COMMAND", U); MessageDispatcher.AddListener("J_COMMAND", J); MessageDispatcher.AddListener("A_COMMAND", A); MessageDispatcher.AddListener("D_COMMAND", D); MessageDispatcher.AddListener("ReplayEvent", ReplayEvent); } protected virtual void OnDestroy() { MessageDispatcher.RemoveListener("EQUALS_COMMAND", PressureUp); MessageDispatcher.RemoveListener("MINUS_COMMAND", PressureDown); MessageDispatcher.RemoveListener("U_COMMAND", U); MessageDispatcher.RemoveListener("J_COMMAND", J); MessageDispatcher.RemoveListener("A_COMMAND", A); MessageDispatcher.RemoveListener("D_COMMAND", D); MessageDispatcher.RemoveListener("ReplayEvent", ReplayEvent); } protected virtual void AddRecordEvent(RecordEventType type) { if (ReplaySetting.PlayStatus == PlayStatus.isEditor && RecordManager.Instance.recordStatus == RecordStatus.normal) { var eventData = new EventData(); eventData.time = RecordManager.Instance.RecordTimer; eventData.cloneObjType = GetComponent().gameObjType; eventData.eventType = type; eventData.json = gameObject.name; RecordManager.Instance.jsonData.eventDataList.Add(eventData); } } protected virtual void ReplayEvent(IMessage obj) { } protected virtual void D(IMessage obj) { } protected virtual void A(IMessage obj) { } protected virtual void J(IMessage obj) { } protected virtual void U(IMessage obj) { } protected virtual void PressureDown(IMessage obj) { } protected virtual void PressureUp(IMessage obj) { } protected virtual void Start() { obj = obj? obj:transform;//如果开始没有物体 赋给本身 System.Random ran = new System.Random(); initTime = Time.time; } protected virtual void Update () { UpdateTransform(); } /// /// 根据设置开始变化 /// protected virtual void UpdateTransform() { switch (transformType) { case TransformType.Position: if (x) PositionChange(obj, Axis.x); if (y) PositionChange(obj, Axis.y); if (z) PositionChange(obj, Axis.z); break; case TransformType.Rotation: if (x) RoatatinChange(obj, Axis.x); if (y) RoatatinChange(obj, Axis.y); if (z) RoatatinChange(obj, Axis.z); break; case TransformType.Scale: if (x) ScaleChange(obj, Axis.x); if (y) ScaleChange(obj, Axis.y); if (z) ScaleChange(obj, Axis.z); break; } } /// /// Transform变化方式 /// protected enum TransformType { Position, Rotation, Scale } /// /// 改变的轴 /// protected enum Axis { x, y, z } #region transform变化类型 private Vector3 vec = new Vector3(); protected virtual void RoatatinChange(Transform transform,Axis axis) { switch (axis) { case Axis.x: vec = new Vector3(ChangeType(min, max,speed), transform.localEulerAngles.y, transform.localEulerAngles.z); break; case Axis.y: vec = new Vector3(transform.localEulerAngles.x, ChangeType(min, max, speed), transform.localEulerAngles.z); break; case Axis.z: vec = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, ChangeType(min, max, speed)); break; } transform.localEulerAngles = vec; } protected virtual void PositionChange(Transform transform, Axis axis) { switch (axis) { case Axis.x: vec = new Vector3(ChangeType(min, max, speed), transform.localPosition.y, transform.localPosition.z); break; case Axis.y: vec = new Vector3(transform.localPosition.x, ChangeType(min, max, speed), transform.localPosition.z); break; case Axis.z: vec = new Vector3(transform.localPosition.x, transform.localPosition.y, ChangeType(min, max, speed)); break; } transform.localPosition = vec; } protected virtual void ScaleChange(Transform transform, Axis axis) { switch (axis) { case Axis.x: vec = new Vector3(ChangeType(min, max, speed), transform.localScale.y, transform.localScale.z); break; case Axis.y: vec = new Vector3(transform.localScale.x, ChangeType(min, max, speed), transform.localScale.z); break; case Axis.z: vec = new Vector3(transform.localScale.x, transform.localScale.y, ChangeType(min, max, speed)); break; } transform.localScale = vec; } #endregion /// /// 数值变化方式 /// protected enum ValueChange { PingPong, Repeat, Random } /// /// 随机时间 /// public float initTime; /// /// 使用的数值变化类型 /// /// min-max之间的数值 protected virtual float ChangeType(float min,float max,float speed) { switch (valueChanage) { case ValueChange.PingPong: return Mathf.Lerp(min, max, Mathf.PingPong((Time.time - initTime) * speed, 1)); case ValueChange.Repeat: return Mathf.Lerp(min, max, Mathf.Repeat((Time.time- initTime) * speed, 1)); case ValueChange.Random: return Mathf.Lerp(min, max, GetRandom()); } return 0; } private float num;//记录上一次的值 private float time;//累计时间 /// /// 获取随机数0-1 /// /// 0-1 private float GetRandom() { time += Time.deltaTime; if (time > changeTime)//频率 { time = 0; System.Random ran = new System.Random(); num = ran.Next(0, 101) / 100f; return num; } return num; } }