|
|
@@ -1,5 +1,9 @@
|
|
|
-using System.Collections;
|
|
|
+using Sirenix.OdinInspector;
|
|
|
+using System;
|
|
|
+using System.Collections;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using UnityEditor;
|
|
|
using UnityEngine;
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -11,11 +15,13 @@ public enum IndependentAniTriigerType
|
|
|
press
|
|
|
}
|
|
|
|
|
|
+[ExecuteAlways]
|
|
|
+[ExecuteInEditMode]
|
|
|
[RequireComponent(typeof(OpTrigger_ToolPack))]
|
|
|
/// <summary>
|
|
|
/// 工具交互-独立动画控制
|
|
|
/// </summary>
|
|
|
-public class ToolPackInteractive_IndependentAni : MonoBehaviour
|
|
|
+public class ToolPackInteractive_IndependentAni : SerializedMonoBehaviour
|
|
|
{
|
|
|
public IndependentAniTriigerType m_IndependentAniTriigerType = IndependentAniTriigerType.press;
|
|
|
[HideInInspector]
|
|
|
@@ -32,17 +38,20 @@ public class ToolPackInteractive_IndependentAni : MonoBehaviour
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public SimpleMoveItem m_TargetSimplaeMoveItem;
|
|
|
public bool isActive;
|
|
|
- private RayCastItem m_CurrentRayCastItem;
|
|
|
+ [HideInInspector]
|
|
|
+ public RayCastItem m_CurrentRayCastItem;
|
|
|
+ private RayCastItem m_LastRayCastItem;
|
|
|
private Dictionary<RayCastItem, SimplaeModelAssociation> mRayCastItemValues = new Dictionary<RayCastItem, SimplaeModelAssociation>();
|
|
|
+ private IndependentAni_BanShou independentAni_BanShou;
|
|
|
public void Init()
|
|
|
{
|
|
|
mRayCastItemValues.Clear();
|
|
|
+ independentAni_BanShou = (toolAnimationBase as IndependentAni_BanShou);
|
|
|
foreach (var item in ToolPack.targetObjs)
|
|
|
{
|
|
|
SimplaeModelAssociation simplaeModelAssociation = new SimplaeModelAssociation();
|
|
|
- foreach (var moveItem in m_TargetSimplaeMoveItem.moveObjs)
|
|
|
+ foreach (var moveItem in independentAni_BanShou.m_SimpleMoveItem.moveObjs)
|
|
|
{
|
|
|
if (Vector3.Distance(moveItem.Key.transform.position, item.transform.position) < 0.01f)
|
|
|
{
|
|
|
@@ -50,16 +59,19 @@ public class ToolPackInteractive_IndependentAni : MonoBehaviour
|
|
|
simplaeModelAssociation.value = 0;
|
|
|
}
|
|
|
}
|
|
|
+ Debug.Log("Init" + item.gameObject.name + "---" + simplaeModelAssociation.model.name);
|
|
|
mRayCastItemValues.Add(item, simplaeModelAssociation);
|
|
|
}
|
|
|
+
|
|
|
+ isActive = true;
|
|
|
}
|
|
|
public void Update()
|
|
|
{
|
|
|
- if (!isActive || !m_TargetSimplaeMoveItem) return;
|
|
|
+ if (!isActive) return;
|
|
|
|
|
|
if (Input.GetMouseButton(0))
|
|
|
{
|
|
|
- m_CurrentRayCastItem = null;
|
|
|
+ m_CurrentRayCastItem = null;
|
|
|
foreach (var item in ToolPack.targetObjs)
|
|
|
{
|
|
|
if (CameraRayCastManager.Instance.CheckCurrentHoverItem(item))
|
|
|
@@ -69,20 +81,386 @@ public class ToolPackInteractive_IndependentAni : MonoBehaviour
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if (!mRayCastItemValues.ContainsKey(m_CurrentRayCastItem)) return;
|
|
|
+ if (m_CurrentRayCastItem == null)
|
|
|
+ {
|
|
|
+ if (isPlaying && mRayCastItemValues.ContainsKey(m_LastRayCastItem))
|
|
|
+ {
|
|
|
+ isPlaying = false;
|
|
|
+ mRayCastItemValues[m_LastRayCastItem].value = independentAni_BanShou.StopPlayBack(this, false);
|
|
|
+
|
|
|
+ m_LastRayCastItem = null;
|
|
|
+ }
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (m_LastRayCastItem != m_CurrentRayCastItem)
|
|
|
+ {
|
|
|
+ if (!isPlaying && m_CurrentRayCastItem != null && mRayCastItemValues.ContainsKey(m_CurrentRayCastItem))
|
|
|
+ {
|
|
|
+ isPlaying = true;
|
|
|
+ independentAni_BanShou.PlayByValue(this, mRayCastItemValues[m_CurrentRayCastItem].value, false);
|
|
|
+ }
|
|
|
+ else if (isPlaying && mRayCastItemValues.ContainsKey(m_LastRayCastItem))
|
|
|
+ {
|
|
|
+ isPlaying = false;
|
|
|
+ mRayCastItemValues[m_LastRayCastItem].value = independentAni_BanShou.StopPlayBack(this, false);
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- mRayCastItemValues[m_CurrentRayCastItem].value += 1 / m_TargetSimplaeMoveItem.duration * Time.deltaTime;
|
|
|
- mRayCastItemValues[m_CurrentRayCastItem].value = Mathf.Clamp(mRayCastItemValues[m_CurrentRayCastItem].value, 0, 1);
|
|
|
- m_TargetSimplaeMoveItem.SetKeyValue(mRayCastItemValues[m_CurrentRayCastItem].model, mRayCastItemValues[m_CurrentRayCastItem].value);
|
|
|
- m_CurrentRayCastItem.transform.position = mRayCastItemValues[m_CurrentRayCastItem].model.transform.position;
|
|
|
- m_CurrentRayCastItem.transform.rotation = mRayCastItemValues[m_CurrentRayCastItem].model.transform.rotation;
|
|
|
+ if (isPlaying && mRayCastItemValues.ContainsKey(m_CurrentRayCastItem))
|
|
|
+ {
|
|
|
+ independentAni_BanShou.PlayUpdate(this, false);
|
|
|
+ m_CurrentRayCastItem.transform.position = mRayCastItemValues[m_CurrentRayCastItem].model.transform.position;
|
|
|
+ m_CurrentRayCastItem.transform.rotation = mRayCastItemValues[m_CurrentRayCastItem].model.transform.rotation;
|
|
|
+ }
|
|
|
+ //mRayCastItemValues[m_CurrentRayCastItem].value += 1 / independentAni_BanShou.duration * Time.deltaTime;
|
|
|
+ //mRayCastItemValues[m_CurrentRayCastItem].value = Mathf.Clamp(mRayCastItemValues[m_CurrentRayCastItem].value, 0, 1);
|
|
|
+ //independentAni_BanShou.SetKeyValue(mRayCastItemValues[m_CurrentRayCastItem].model, mRayCastItemValues[m_CurrentRayCastItem].value);
|
|
|
+ //m_CurrentRayCastItem.transform.position = mRayCastItemValues[m_CurrentRayCastItem].model.transform.position;
|
|
|
+ //m_CurrentRayCastItem.transform.rotation = mRayCastItemValues[m_CurrentRayCastItem].model.transform.rotation;
|
|
|
|
|
|
- if (mRayCastItemValues[m_CurrentRayCastItem].value == 1)
|
|
|
+ if (mRayCastItemValues.ContainsKey(m_CurrentRayCastItem) && mRayCastItemValues[m_CurrentRayCastItem].value == 1)
|
|
|
{
|
|
|
mRayCastItemValues[m_CurrentRayCastItem].model.SetActive(false);
|
|
|
}
|
|
|
+
|
|
|
+ m_LastRayCastItem = m_CurrentRayCastItem;
|
|
|
+ }
|
|
|
+ else if (Input.GetMouseButtonUp(0))
|
|
|
+ {
|
|
|
+ if (isPlaying && mRayCastItemValues.ContainsKey(m_CurrentRayCastItem))
|
|
|
+ {
|
|
|
+ mRayCastItemValues[m_CurrentRayCastItem].value = independentAni_BanShou.StopPlayBack(this, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ m_CurrentRayCastItem = null;
|
|
|
+ m_LastRayCastItem = null;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ public void CurrentFinished()
|
|
|
+ {
|
|
|
+ if (mRayCastItemValues.ContainsKey(m_CurrentRayCastItem))
|
|
|
+ {
|
|
|
+ mRayCastItemValues[m_CurrentRayCastItem].value = 1;
|
|
|
+ Debug.Log(m_CurrentRayCastItem.name + "Value=1");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool IsFinished()
|
|
|
+ {
|
|
|
+ foreach (var item in mRayCastItemValues)
|
|
|
+ {
|
|
|
+ if (item.Value.value < 1)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 模型及工具锚点参数
|
|
|
+ [FoldoutGroup("模型及锚点", Order = -1)]
|
|
|
+ [HorizontalGroup("模型及锚点/模型组")]
|
|
|
+ [LabelText("模型")]
|
|
|
+ [LabelWidth(30)]
|
|
|
+ public GameObject model;
|
|
|
+ [FoldoutGroup("模型及锚点", Order = -1)]
|
|
|
+ [LabelText("锚点")]
|
|
|
+ [LabelWidth(30)]
|
|
|
+ [HorizontalGroup("模型及锚点/模型组")]
|
|
|
+ public Transform modelAnchor;
|
|
|
+
|
|
|
+ [FoldoutGroup("工具及锚点", Order = -1)]
|
|
|
+ [LabelText("工具")]
|
|
|
+ [LabelWidth(30)]
|
|
|
+ [HorizontalGroup("工具及锚点/模型组")]
|
|
|
+ public GameObject toolObj;
|
|
|
+ [FoldoutGroup("工具及锚点", Order = -1)]
|
|
|
+ [LabelText("锚点")]
|
|
|
+ [LabelWidth(30)]
|
|
|
+ [HorizontalGroup("工具及锚点/模型组")]
|
|
|
+ public Transform toolAnchor;
|
|
|
+ private GameObject toolPrefab;
|
|
|
+ [FoldoutGroup("工具及锚点", Order = -1)]
|
|
|
+ [InfoBox("工具预支物路径:Assets/ChivaFramework/Framework/SimulationToolDev/ToolManager/ToolModel")]
|
|
|
+ [ValueDropdown("GetAllToolModel")]
|
|
|
+ [ShowInInspector]
|
|
|
+ [LabelText("加载模型预支物")]
|
|
|
+ public GameObject ToolPrefab
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ return toolPrefab;
|
|
|
+ }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ if (toolPrefab != value)
|
|
|
+ {
|
|
|
+ toolPrefab = value;
|
|
|
+ if (toolObj != null)
|
|
|
+ {
|
|
|
+ DestroyImmediate(toolObj);
|
|
|
+ }
|
|
|
+ toolObj = GameObject.Instantiate(toolPrefab);
|
|
|
+
|
|
|
+ if (modelAnchor != null)
|
|
|
+ {
|
|
|
+ if (toolAnchor == null)
|
|
|
+ {
|
|
|
+ toolAnchor = new GameObject("工具锚点(该锚点位置会强制刷新,若需调整请调整子级工具预支物相对位置)").transform;
|
|
|
+ toolAnchor.transform.parent = modelAnchor.transform;
|
|
|
+ toolAnchor.transform.localPosition = Vector3.zero;
|
|
|
+ toolAnchor.transform.localRotation = Quaternion.identity;
|
|
|
+ toolAnchor.transform.localScale = Vector3.one;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ toolAnchor.name = "工具锚点(该锚点位置会强制刷新,若需调整请调整子级工具预支物相对位置)";
|
|
|
+ toolAnchor.transform.parent = modelAnchor.transform;
|
|
|
+ toolAnchor.transform.localPosition = Vector3.zero;
|
|
|
+ toolAnchor.transform.localRotation = Quaternion.identity;
|
|
|
+ toolAnchor.transform.localScale = Vector3.one;
|
|
|
+ }
|
|
|
+
|
|
|
+ toolObj.transform.parent = toolAnchor.transform;
|
|
|
+ toolObj.transform.localPosition = Vector3.zero;
|
|
|
+ toolObj.transform.localRotation = Quaternion.identity;
|
|
|
+ toolObj.transform.localScale = Vector3.one;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+#if UNITY_EDITOR
|
|
|
+ private static IEnumerable GetAllToolModel()
|
|
|
+ {
|
|
|
+ var root = "Assets/ChivaFramework/Framework/SimulationToolDev/ToolManager/ToolModel";
|
|
|
+
|
|
|
+ return UnityEditor.AssetDatabase.GetAllAssetPaths()
|
|
|
+ .Where(x => x.StartsWith(root))
|
|
|
+ .Select(x => x.Substring(root.Length))
|
|
|
+ .Select(x => new ValueDropdownItem(x, UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.GameObject>(root + x)));
|
|
|
+ }
|
|
|
+#endif
|
|
|
+
|
|
|
+ public bool ToolActiveState
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (toolAnchor == null)
|
|
|
+ {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return toolAnchor.gameObject.activeSelf;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ [FoldoutGroup("工具及锚点", Order = -1)]
|
|
|
+ [Button("$ToolBtnName")]
|
|
|
+ public void ToolActive()
|
|
|
+ {
|
|
|
+ if (toolAnchor != null)
|
|
|
+ {
|
|
|
+ toolAnchor.gameObject.SetActive(!toolAnchor.gameObject.activeSelf);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public string ToolBtnName
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (ToolActiveState)
|
|
|
+ {
|
|
|
+ return "隐藏工具";
|
|
|
+ }
|
|
|
+ else { return "显示工具"; }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ [SerializeField]
|
|
|
+ [HideInInspector]
|
|
|
+ private IndependentAniBase toolAnimationBase;
|
|
|
+ [BoxGroup("工具动画")]
|
|
|
+ [HideLabel]
|
|
|
+ [ShowInInspector]
|
|
|
+ [TypeFilter("GetFilteredTypeList")]
|
|
|
+ public IndependentAniBase ToolAnimationBase
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ return toolAnimationBase;
|
|
|
+ }
|
|
|
+ set
|
|
|
+ {
|
|
|
+ if (toolAnimationBase != value)
|
|
|
+ {
|
|
|
+ toolAnimationBase = value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public IEnumerable<Type> GetFilteredTypeList()
|
|
|
+ {
|
|
|
+ var q = typeof(IndependentAniBase).Assembly.GetTypes()
|
|
|
+ .Where(x => !x.IsAbstract)
|
|
|
+ .Where(x => !x.IsGenericTypeDefinition)
|
|
|
+ .Where(x => typeof(IndependentAniBase).IsAssignableFrom(x));
|
|
|
+
|
|
|
+ return q;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void SetInitState(bool reverse)
|
|
|
+ {
|
|
|
+ isPlaying = false;
|
|
|
+ if (toolAnimationBase != null)
|
|
|
+ {
|
|
|
+ toolAnimationBase.InitState(this, reverse);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public void SetFinishedState(bool reverse)
|
|
|
+ {
|
|
|
+ isPlaying = false;
|
|
|
+ if (toolAnimationBase != null)
|
|
|
+ {
|
|
|
+ toolAnimationBase.FinishedState(this, reverse);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool PlayToolAnimation(bool reverse)
|
|
|
+ {
|
|
|
+ if (toolAnimationBase != null)
|
|
|
+ {
|
|
|
+ toolAnimationBase.PlayUpdate(this, reverse);
|
|
|
+ }
|
|
|
+ return isPlaying;
|
|
|
+ }
|
|
|
+
|
|
|
+ void Awake()
|
|
|
+ {
|
|
|
+ if (toolAnchor.gameObject.activeSelf)
|
|
|
+ {
|
|
|
+ toolAnchor.gameObject.SetActive(false);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public bool editorReverse = false;
|
|
|
+
|
|
|
+ [Button("$PlayBtnName")]
|
|
|
+ public void Play()
|
|
|
+ {
|
|
|
+ if (!isPlaying)
|
|
|
+ {
|
|
|
+ isPlaying = true;
|
|
|
+ if (toolAnimationBase != null)
|
|
|
+ {
|
|
|
+ toolAnimationBase.PlayTrigger(this, editorReverse);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ isPlaying = false;
|
|
|
+ if (toolAnimationBase != null)
|
|
|
+ {
|
|
|
+ toolAnimationBase.StopPlay(this, editorReverse);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public string PlayBtnName
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ if (isPlaying)
|
|
|
+ {
|
|
|
+ return "停止";
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return "播放";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public bool isPlaying;
|
|
|
+
|
|
|
+ public void Destroy(GameObject obj)
|
|
|
+ {
|
|
|
+ if (obj != null)
|
|
|
+ {
|
|
|
+ DestroyImmediate(obj);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+#if UNITY_EDITOR
|
|
|
+
|
|
|
+ [FoldoutGroup("模型及锚点", Order = -1)]
|
|
|
+ [Button("重置模型")]
|
|
|
+ public void AddSelectObj()
|
|
|
+ {
|
|
|
+ if (Selection.gameObjects.Length == 0) return;
|
|
|
+
|
|
|
+ model = Selection.activeGameObject;
|
|
|
+ if (modelAnchor != null)
|
|
|
+ {
|
|
|
+ //DestroyImmediate(modelAnchor.gameObject);
|
|
|
+ modelAnchor.name = "锚点_" + model.gameObject.name;
|
|
|
+ modelAnchor.transform.parent = transform;
|
|
|
+ modelAnchor.transform.position = model.transform.position;
|
|
|
+ modelAnchor.transform.rotation = model.transform.rotation;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ GameObject tmpItem = new GameObject("锚点_" + model.gameObject.name);
|
|
|
+ tmpItem.transform.parent = transform;
|
|
|
+ tmpItem.transform.position = model.transform.position;
|
|
|
+ tmpItem.transform.rotation = model.transform.rotation;
|
|
|
+ modelAnchor = tmpItem.transform;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void OnDrawGizmos()
|
|
|
+ {
|
|
|
+ if (ToolAnimationBase != null)
|
|
|
+ {
|
|
|
+ ToolAnimationBase.OnDrawGizmos(this);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 销毁所有子级信息
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="item"></param>
|
|
|
+ private void DestroyChild(Transform item)
|
|
|
+ {
|
|
|
+ if (item.childCount == 0) return;
|
|
|
+
|
|
|
+ for (int i = item.childCount - 1; i >= 0; i--)
|
|
|
+ {
|
|
|
+ DestroyImmediate(item.GetChild(i).gameObject);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void OnEnable()
|
|
|
+ {
|
|
|
+ EditorApplication.update += EditorUpdate;
|
|
|
+ }
|
|
|
+ private void OnDisable()
|
|
|
+ {
|
|
|
+ EditorApplication.update -= EditorUpdate;
|
|
|
+ }
|
|
|
+
|
|
|
+ private float timer = 0;
|
|
|
+ void EditorUpdate()
|
|
|
+ {
|
|
|
+ if (!isPlaying || Application.isPlaying) return;
|
|
|
+ toolAnimationBase.EditorPlayUpdate(this, editorReverse);
|
|
|
+ }
|
|
|
+#endif
|
|
|
}
|
|
|
|
|
|
public class SimplaeModelAssociation
|