大连中石油电子沙盘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.6 KiB

4 years ago
using UnityEngine;
using AX.MessageSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.EventSystems;
//Author:ZCG
//CreatTime:12/2/2017
/// <summary>
///<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Transform
/// </summary>
public class BaseTransformChange : MonoBehaviour
{
[SerializeField]
protected Transform obj;//<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[SerializeField]
protected TransformType transformType;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD>ת <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[SerializeField]
protected ValueChange valueChanage;//<EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>ʽ ƹ<EFBFBD><EFBFBD> <EFBFBD>ظ<EFBFBD>
//[SerializeField]
//protected Axis axis = Axis.x;//<EFBFBD><EFBFBD>
[SerializeField]
protected float min = 0;//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[SerializeField]
protected float max = 1;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
[SerializeField]
[Tooltip("Repat/PingPong<EFBFBD><EFBFBD>ٶ<EFBFBD>")]
[Range(0.1f,10f)]
protected float speed = 1;//<EFBFBD>ٶ<EFBFBD>
[SerializeField]
[Tooltip("Random<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>")]
[Range(0f,100f)]
private float changeTime = 1;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĸı<EFBFBD>Ƶ<EFBFBD><EFBFBD>
[SerializeField]
protected bool x, y, z;//<EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD>
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<BaseGameObjInfo>().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;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼû<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
System.Random ran = new System.Random();
initTime = Time.time;
}
protected virtual void Update ()
{
UpdateTransform();
}
/// <summary>
/// <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ÿ<EFBFBD>ʼ<EFBFBD>
/// </summary>
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;
}
}
/// <summary>
/// Transform<EFBFBD><EFBFBD><EFBFBD>ʽ
/// </summary>
protected enum TransformType
{
Position,
Rotation,
Scale
}
/// <summary>
/// <EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
protected enum Axis
{
x, y, z
}
#region transform<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
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
/// <summary>
/// <EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD>ʽ
/// </summary>
protected enum ValueChange
{
PingPong,
Repeat,
Random
}
/// <summary>
/// <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
/// </summary>
public float initTime;
/// <summary>
/// ʹ<EFBFBD>õ<EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <returns>min-max֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ</returns>
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;//<EFBFBD><EFBFBD>¼<EFBFBD><EFBFBD>һ<EFBFBD>ε<EFBFBD>ֵ
private float time;//<EFBFBD>ۼ<EFBFBD>ʱ<EFBFBD><EFBFBD>
/// <summary>
/// <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0-1
/// </summary>
/// <returns>0-1</returns>
private float GetRandom()
{
time += Time.deltaTime;
if (time > changeTime)//Ƶ<EFBFBD><EFBFBD>
{
time = 0;
System.Random ran = new System.Random();
num = ran.Next(0, 101) / 100f;
return num;
}
return num;
}
}