簡易的門制作
對于一個新手來說,這個工具是最好的選擇
上一篇關(guān)于開關(guān)門的文章相對于復(fù)雜,感興趣的可以查看上篇開關(guān)門制作
優(yōu)點
- 掛載就能使用
- 控制面板一看就懂(全是中文)
- 簡單的調(diào)試就能獲得自己想要的效果
- 易懂且易修改的代碼
面板
參數(shù)
- 鎖:勾選后不能對門進(jìn)行操作
- 聲音:激活時自動播放
- 激活:測試開關(guān)門
- 查看結(jié)果位置:按住查看結(jié)果,松開返回
旋轉(zhuǎn)門的使用不做介紹
推拉門(推拉抽屜):
- 起始位置:門的初始位置
- 結(jié)束位置:門要移動的位置
- 得到位置按鈕:當(dāng)你在場景中調(diào)整門的位置后,把當(dāng)前門的位置賦值到左側(cè)
代碼
掛載之后會自動添加Audio Source組件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(AudioSource))]
public class Door_Interaction : MonoBehaviour
{
public enum doorType { RotatingDoor = 0, SlidingDoor = 1 }
public doorType doorMovementType;
public bool open;
public bool locked;
public float rotateAngle = 90;
public float speed = 1;
public enum axis { X = 0, Y = 1, Z = 2 }
public axis rotateAxis = axis.Y;
public Vector3 startingPosition;
public Vector3 endingPosition;
public AudioClip startClip;
public AudioClip endClip;
private AudioSource sound;
public Quaternion originRotation;
public Quaternion openRotation;
private float r;
private bool opening;
[Tooltip("激活測試")]
public bool activate = false;
void Start()
{
InitRotation();
sound = GetComponent<AudioSource>();
r = 0;
}
public void InitRotation()
{
originRotation = transform.rotation;
openRotation = originRotation;
if (rotateAxis == axis.Y)
{
openRotation = Quaternion.Euler(new Vector3(originRotation.eulerAngles.x, originRotation.eulerAngles.y + rotateAngle, originRotation.eulerAngles.z));
}
else if (rotateAxis == axis.X)
{
openRotation = Quaternion.Euler(new Vector3(originRotation.eulerAngles.x + rotateAngle, originRotation.eulerAngles.y, originRotation.eulerAngles.z));
}
else if (rotateAxis == axis.Z)
{
openRotation = Quaternion.Euler(new Vector3(originRotation.eulerAngles.x, originRotation.eulerAngles.y, originRotation.eulerAngles.z + rotateAngle));
}
}
void Update()
{
if (activate)
{
activate = false;
if (opening == false && locked == false)
{
open = !open;
r = 0;
opening = true;
}
}
if (opening)
{
ChangeState(open);
}
}
void ChangeState(bool State)
{
Quaternion quaternion;
Vector3 vector3;
if (State)
{
quaternion = openRotation;
vector3 = endingPosition;
if (startClip!=null&& r==0)
{
sound.clip = startClip;
sound.Play();
}
}
else
{
quaternion = originRotation;
vector3 = startingPosition;
if (endClip != null && r == 0)
{
sound.clip = endClip;
sound.Play();
}
}
r += Time.deltaTime * speed;
if (doorMovementType == doorType.RotatingDoor)
{
if (Quaternion.Angle(quaternion, transform.rotation) > 0.1F)
{
transform.rotation = Quaternion.Slerp(transform.rotation, quaternion, r);
}
else
{
transform.rotation = quaternion;
r = 0;
opening = false;
}
}
else if (doorMovementType == doorType.SlidingDoor)
{
if (Vector3.Distance(transform.localPosition, vector3) > 0.005F)
{
transform.localPosition = Vector3.Lerp(transform.localPosition, vector3, r);
}
else
{
transform.localPosition = vector3;
r = 0;
opening = false;
}
}
}
}
編輯器擴展代碼
需要注意!這個代碼必須放在Editor文件下才能正常使用,Unity會自己處理Editor文件夾,就像Resources文件夾一樣
我之前寫的通用相機控制器也是一樣的使用方法
感興趣的可以查看通用相機控制器文章來源:http://www.zghlxwxcb.cn/news/detail-424166.html
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Door_Interaction)), CanEditMultipleObjects]
public class Door_Interaction_Editor : Editor
{
private SerializedProperty open;
private SerializedProperty locked;
private SerializedProperty rotateAngle;
private SerializedProperty speed;
private SerializedProperty rotateAxis;
private SerializedProperty startingPosition;
private SerializedProperty endingPosition;
private SerializedProperty activate;
private SerializedProperty startClip;
private SerializedProperty endClip;
private GUIStyle defaultStyle = new GUIStyle();
public void OnEnable()
{
open = serializedObject.FindProperty("open");
locked = serializedObject.FindProperty("locked");
rotateAngle = serializedObject.FindProperty("rotateAngle");
speed = serializedObject.FindProperty("speed");
rotateAxis = serializedObject.FindProperty("rotateAxis");
startingPosition = serializedObject.FindProperty("startingPosition");
endingPosition = serializedObject.FindProperty("endingPosition");
activate = serializedObject.FindProperty("activate");
startClip = serializedObject.FindProperty("startClip");
endClip = serializedObject.FindProperty("endClip");
}
bool IsButtonDown = false;
Vector3 CurrentPosition= Vector3.zero;
Quaternion CurrentRotation = Quaternion.identity;
public override void OnInspectorGUI()
{
serializedObject.Update();
var myScript = target as Door_Interaction;
var doorGUIContent = new GUIContent("門類型", "選擇不同門的類型參數(shù)也有所改變");
GUIContent[] doorOptions;
doorOptions = new[] { new GUIContent("旋轉(zhuǎn)門"), new GUIContent("推拉門") };
myScript.doorMovementType = (Door_Interaction.doorType)EditorGUILayout.Popup(doorGUIContent, (int)myScript.doorMovementType, doorOptions);
EditorGUILayout.PropertyField(open,new GUIContent("當(dāng)前狀態(tài)"));
EditorGUILayout.PropertyField(locked, new GUIContent("鎖"));
EditorGUILayout.PropertyField(speed, new GUIContent("執(zhí)行速度"));
EditorGUILayout.PropertyField(startClip, new GUIContent("打開時的聲音"));
EditorGUILayout.PropertyField(endClip, new GUIContent("關(guān)閉時的聲音"));
EditorGUILayout.PropertyField(activate, new GUIContent("激活"));
GUILayout.Space(20);//間隔
if (myScript.doorMovementType == Door_Interaction.doorType.RotatingDoor) {
EditorGUILayout.PropertyField(rotateAngle, new GUIContent("旋轉(zhuǎn)角度"));
EditorGUILayout.PropertyField(rotateAxis, new GUIContent("軸", "門的旋轉(zhuǎn)軸。 默認(rèn)是Y "));
GUILayout.Space(20);//間隔
if (GUILayout.RepeatButton(new GUIContent("查看結(jié)束位置", "在視圖中查看結(jié)束位置")))
{
if (IsButtonDown == false)
{
IsButtonDown = true;
myScript.InitRotation();
CurrentRotation = myScript.transform.localRotation;
}
myScript.transform.rotation = myScript.openRotation;
}
else
{
if (IsButtonDown)
{
IsButtonDown = false;
myScript.transform.localRotation = CurrentRotation;
}
}
} else if (myScript.doorMovementType == Door_Interaction.doorType.SlidingDoor) {
defaultStyle.alignment = TextAnchor.UpperCenter; //字體對齊方式: 水平靠左,垂直居中
defaultStyle.normal.textColor = Color.yellow; //字體顏色:黃色
defaultStyle.fontSize = 15; //字體大?。?20
GUILayout.Label("起始位置", defaultStyle);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(startingPosition, new GUIContent(""));
if (GUILayout.Button(new GUIContent("得到位置", "將當(dāng)前的局部坐標(biāo)賦值給開始位置")))
{
myScript.startingPosition = myScript.transform.localPosition;
}
EditorGUILayout.EndHorizontal();
GUILayout.Label("結(jié)束位置", defaultStyle);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(endingPosition, new GUIContent(""));
if (GUILayout.Button(new GUIContent("得到位置", "將當(dāng)前的局部坐標(biāo)賦值給結(jié)束位置")))
{
myScript.endingPosition = myScript.transform.localPosition;
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(20);//間隔
if (GUILayout.RepeatButton(new GUIContent("查看結(jié)束位置", "在視圖中查看結(jié)束位置")))
{
if (IsButtonDown==false)
{
IsButtonDown = true;
CurrentPosition = myScript.transform.localPosition;
}
myScript.transform.localPosition = myScript.endingPosition;
}
else
{
if (IsButtonDown)
{
IsButtonDown = false;
myScript.transform.localPosition = CurrentPosition;
}
}
}
serializedObject.ApplyModifiedProperties();//顧名思義 應(yīng)用修改的屬性
if (GUI.changed == true) {
EditorUtility.SetDirty(target);//這個函數(shù)告訴引擎,相關(guān)對象所屬于的Prefab已經(jīng)發(fā)生了更改。
}
}
}
別要Demo了,按照流程賦值粘貼代碼就能用 !文章來源地址http://www.zghlxwxcb.cn/news/detail-424166.html
到了這里,關(guān)于Unity 制作旋轉(zhuǎn)門 推拉門 柜門 抽屜 點擊自動開門效果 開關(guān)門自動播放音效 (附帶編輯器擴展代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!