Tương tác

0
187

Tương tác
Đã kiểm tra với phiên bản: 5.5
– –
Độ khó: Trung cấp
Giai đoạn 5 tiếp tục dạy bạn cách xây dựng một hệ thống tương tác, tập trung vào các tương tác. Chúng tôi sẽ tạo ra một hệ thống để xác định những gì người chơi có thể tương tác với bao gồm cả hình học tương tác, sử dụng EventSystem và đưa ra một bản tóm tắt hệ thống tương tác. Tải xuống gói nội dung cho Giai đoạn tại đây.https://www.assetstore.unity3d.com/en/?&_ga=2.167546430.1082230255.1536856804-659907597.1536596969#!/content/76789


Tải xuống gói nội dung cho Giai đoạn 5 tại Cửa hàng tài sản hợp nhất tại đây.https://www.assetstore.unity3d.com/en/?&_ga=2.133145998.1082230255.1536856804-659907597.1536596969#!/content/76789

Interactable

Expand view

Copy code
using UnityEngine;
public class Interactable : MonoBehaviour
{
public Transform interactionLocation;
public ConditionCollection[] conditionCollections = new ConditionCollection[0];
public ReactionCollection defaultReactionCollection;
public void Interact ()
{
for (int i = 0; i < conditionCollections.Length; i++)
{
if (conditionCollections[i].CheckAndReact ())
return;
}
defaultReactionCollection.React ();
}
}


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


InteractableEditor

Expand view

Copy code
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Interactable))]
public class InteractableEditor : EditorWithSubEditors<ConditionCollectionEditor, ConditionCollection>
{
private Interactable interactable;
private SerializedProperty interactionLocationProperty;
private SerializedProperty collectionsProperty;
private SerializedProperty defaultReactionCollectionProperty;
private const float collectionButtonWidth = 125f;
private const string interactablePropInteractionLocationName = "interactionLocation";
private const string interactablePropConditionCollectionsName = "conditionCollections";
private const string interactablePropDefaultReactionCollectionName = "defaultReactionCollection";
private void OnEnable ()
{
interactable = (Interactable)target;
collectionsProperty = serializedObject.FindProperty(interactablePropConditionCollectionsName);
interactionLocationProperty = serializedObject.FindProperty(interactablePropInteractionLocationName);
defaultReactionCollectionProperty = serializedObject.FindProperty(interactablePropDefaultReactionCollectionName);

CheckAndCreateSubEditors(interactable.conditionCollections);
}
private void OnDisable ()
{
CleanupEditors ();
}
protected override void SubEditorSetup(ConditionCollectionEditor editor)
{
editor.collectionsProperty = collectionsProperty;
}
public override void OnInspectorGUI ()
{
serializedObject.Update ();

CheckAndCreateSubEditors(interactable.conditionCollections);

EditorGUILayout.PropertyField (interactionLocationProperty);
for (int i = 0; i < subEditors.Length; i++)
{
subEditors[i].OnInspectorGUI ();
EditorGUILayout.Space ();
}
EditorGUILayout.BeginHorizontal();
GUILayout.FlexibleSpace ();
if (GUILayout.Button("Add Collection", GUILayout.Width(collectionButtonWidth)))
{
ConditionCollection newCollection = ConditionCollectionEditor.CreateConditionCollection ();
collectionsProperty.AddToObjectArray (newCollection) ;
}
EditorGUILayout.EndHorizontal ();
EditorGUILayout.Space ();
EditorGUILayout.PropertyField (defaultReactionCollectionProperty);
serializedObject.ApplyModifiedProperties ();
}
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here