今天上的是一个我们会经常使用的功能---物体旋转(包括UI对象和游戏对象)。使用方法很简单,将脚本挂在目标对象上,在Inspector面板上添加和选中具体参数即可,如下图所示:
下面上功能代码;还有一份编辑器代码。
using UnityEngine;
/// <summary>
/// 直接挂在需要旋转的对象上
/// 提供旋转的功能
/// </summary>
public class RotateFunction : MonoBehaviour
{
/// <summary>
/// 旋转模式
/// </summary>
public RotateMode MyRotateMode;
/// <summary>
/// 旋转轴
/// </summary>
public RotateAxle MyRotate_Axle;
/// <summary>
/// 绕某目标旋转时的目标对象
/// </summary>
public GameObject RotateTargetObj;
Transform mytransform;
void Start()
{
mytransform = this.transform;
}
//旋转的速度
public float RotateSpeed = 360f;
void FixedUpdate()
{
//自转
RotateAroundSelf();
//绕某点转
RotateAroundTarget();
}
/// <summary>
/// 自传
/// </summary>
void RotateAroundSelf()
{
if (MyRotateMode == RotateMode.Rotate_Self)
{
switch (MyRotate_Axle)
{
case RotateAxle.X:
mytransform.Rotate(new Vector3(1f, 0, 0) * RotateSpeed * Time.deltaTime, Space.Self);
break;
case RotateAxle.Y:
mytransform.Rotate(new Vector3(0, 1f, 0) * RotateSpeed * Time.deltaTime, Space.Self);
break;
case RotateAxle.Z:
mytransform.Rotate(new Vector3(0, 0, 1f) * RotateSpeed * Time.deltaTime, Space.Self);
break;
}
}
}
/// <summary>
/// 绕目标旋转
/// </summary>
void RotateAroundTarget()
{
if (MyRotateMode == RotateMode.Rotate_Target && RotateTargetObj != null)
{
switch (MyRotate_Axle)
{
case RotateAxle.X:
mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.right, RotateSpeed * Time.deltaTime);
break;
case RotateAxle.Y:
mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.up, RotateSpeed * Time.deltaTime);
break;
case RotateAxle.Z:
mytransform.RotateAround(RotateTargetObj.transform.position, Vector3.forward, RotateSpeed * Time.deltaTime);
break;
}
}
}
}
下面上功能编辑器脚本
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
[CustomEditor(typeof(RotateFunction))]
public class RotateFunctionEditor : Editor
{
public RotateFunction m_RotateFunction = null;
public void OnEnable()
{
m_RotateFunction = (RotateFunction)target;
}
public override void OnInspectorGUI()
{
bool isChanged = false;
//绘制MyRotateMode
RotateMode oldMyRotateMode = m_RotateFunction.MyRotateMode;
m_RotateFunction.MyRotateMode = (RotateMode)EditorGUILayout.EnumPopup("MyRotateMode", m_RotateFunction.MyRotateMode);
if (oldMyRotateMode != m_RotateFunction.MyRotateMode)
isChanged = true;
//根据选择的RotateMode来确定RotateTarget
if (m_RotateFunction.MyRotateMode == RotateMode.Rotate_Target)
{
GameObject oidObj = m_RotateFunction.RotateTargetObj;
m_RotateFunction.RotateTargetObj = (GameObject)EditorGUILayout.ObjectField("RotateTargetObj", m_RotateFunction.RotateTargetObj, typeof(GameObject), true);
if (oidObj != m_RotateFunction.RotateTargetObj)
isChanged = true;
}
//绘制MyRotate_Axle
RotateAxle oldMyRotate_Axle = m_RotateFunction.MyRotate_Axle;
m_RotateFunction.MyRotate_Axle = (RotateAxle)EditorGUILayout.EnumPopup("MyRotate_Axle", m_RotateFunction.MyRotate_Axle);
if (oldMyRotate_Axle != m_RotateFunction.MyRotate_Axle)
isChanged = true;
//绘制Rotate Speed
float oldRotateSpeed = m_RotateFunction.RotateSpeed;
m_RotateFunction.RotateSpeed = EditorGUILayout.FloatField("Rotate Speed", m_RotateFunction.RotateSpeed);
if (oldRotateSpeed != m_RotateFunction.RotateSpeed)
isChanged = true;
if (isChanged)
EditorUtility.SetDirty(m_RotateFunction);
}
}
#endif
其实功能脚本倒没啥技术含量,值得初学者看看的是第二份是功能的编辑器脚本!