2026-05-07 10:14:44 +02:00

169 lines
4.5 KiB
C#

using UnityEngine;
using System.Collections;
using Wing.Gizmos;
using UnityEngine.UI;
public class Sample : MonoBehaviour
{
public GameObject Target;
private GameObject mCurrentGizmos;
public Dropdown GizmosTypes;
public Toggle SpanToggle;
public InputField Precision;
public GameObject SnapPanel;
private EGizmosType mGizmosType = EGizmosType.Move;
private bool mMoveInitialed = false;
private bool mScaleInitialed = false;
private bool mRotateInitialed = false;
// Use this for initialization
void Start()
{
onTypeChanged((int)mGizmosType);
onSnapEnableChanged(false);
GizmosTypes.onValueChanged.AddListener(onTypeChanged);
SpanToggle.onValueChanged.AddListener(onSnapEnableChanged);
Precision.onValueChanged.AddListener(onPrecisionChanged);
}
private void onTypeChanged(int id)
{
mGizmosType = (EGizmosType)id;
SetGizmos(mGizmosType);
switch (mGizmosType)
{
case EGizmosType.Move:
Precision.text = SnapFactory.Instance.Get(ESnapType.Move).Precision.ToString();
break;
case EGizmosType.Scale:
Precision.text = SnapFactory.Instance.Get(ESnapType.Scale).Precision.ToString();
break;
case EGizmosType.Rotate:
Precision.text = SnapFactory.Instance.Get(ESnapType.Rotate).Precision.ToString();
break;
default:
break;
}
}
private void onPrecisionChanged(string value)
{
float result = 0;
if (float.TryParse(value, out result))
{
switch (mGizmosType)
{
case EGizmosType.Move:
SnapFactory.Instance.Get(ESnapType.Move).Precision = result;
break;
case EGizmosType.Scale:
SnapFactory.Instance.Get(ESnapType.Scale).Precision = result;
break;
case EGizmosType.Rotate:
SnapFactory.Instance.Get(ESnapType.Rotate).Precision = result;
break;
default:
break;
}
}
}
private void onSnapEnableChanged(bool value)
{
SnapFactory.Instance.Get(ESnapType.Move).Enable = value;
SnapFactory.Instance.Get(ESnapType.Scale).Enable = value;
SnapFactory.Instance.Get(ESnapType.Rotate).Enable = value;
SnapPanel.SetActive(value);
}
// Update is called once per frame
void Update()
{
}
#region Gizmos
private void SetGizmos(EGizmosType gtype)
{
if (mCurrentGizmos != null)
{
mCurrentGizmos.SetActive(false);
}
switch (gtype)
{
case EGizmosType.Move:
mCurrentGizmos = SetMove();
break;
case EGizmosType.Scale:
mCurrentGizmos = SetScale();
break;
case EGizmosType.Rotate:
mCurrentGizmos = SetRotate();
break;
default:
break;
}
if (mCurrentGizmos != null)
{
mCurrentGizmos.transform.position = Target.transform.position;
mCurrentGizmos.transform.localRotation = Target.transform.localRotation;
mCurrentGizmos.SetActive(true);
}
}
private GameObject SetMove()
{
var move = GizmosManager.Instance.GetSingleMove();
if (!mMoveInitialed)
{
move.OnMove += (dx, dy, dz) =>
{
Target.transform.Translate(dx, dy, dz);
};
mMoveInitialed = true;
}
return move.gameObject;
}
private GameObject SetScale()
{
var scale = GizmosManager.Instance.GetSingleScale();
if (!mScaleInitialed)
{
scale.OnScale += (dx, dy, dz) =>
{
Target.transform.localScale += new Vector3(dx, dy, dz);
};
mScaleInitialed = true;
}
return scale.gameObject;
}
private GameObject SetRotate()
{
var rotate = GizmosManager.Instance.GetSingleRotate();
rotate.EnableFreeRotate = true;
if (!mRotateInitialed)
{
rotate.OnRotate += (dx, dy, dz) =>
{
Target.transform.Rotate(new Vector3(dx, dy, dz));
};
mRotateInitialed = true;
}
return rotate.gameObject;
}
#endregion
}