Home / khác / Phản ứng

Phản ứng

Phản ứng
Đã kiểm tra với phiên bản: 5.5
– –
Độ khó: Trung cấp
Giai đoạn 4 tiếp tục dạy cách xây dựng một hệ thống tương tác tập trung vào các phản ứng. Chúng tôi sẽ tạo ra một hệ thống để thực hiện các hành động dựa trên trạng thái điều kiện bằng cách sử dụng đa hình, tiếp tục viết kịch bản và thảo luận về việc tuần tự hóa. Tải xuống gói nội dung cho Giai đoạn 4 tại đây.https://www.assetstore.unity3d.com/en/?&_ga=2.154116536.1082230255.1536856804-659907597.1536596969#!/content/76787
Tải xuống gói nội dung cho Giai đoạn 4 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.134062606.1082230255.1536856804-659907597.1536596969#!/content/76787

ReactionCollectionEditor

Expand view

Copy code
using System;
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
[CustomEditor(typeof(ReactionCollection))]public class ReactionCollectionEditor : EditorWithSubEditors<ReactionEditor, Reaction>
{
private ReactionCollection reactionCollection;
private SerializedProperty reactionsProperty;
private Type[] reactionTypes;
private string[] reactionTypeNames;
private int selectedIndex;
private const float dropAreaHeight = 50f;
private const float controlSpacing = 5f;
private const string reactionsPropName = "reactions";
private readonly float verticalSpacing = EditorGUIUtility.standardVerticalSpacing;
private void OnEnable ()
{
reactionCollection = (ReactionCollection)target;
reactionsProperty = serializedObject.FindProperty(reactionsPropName);
CheckAndCreateSubEditors (reactionCollection.reactions);
SetReactionNamesArray ();
}
private void OnDisable ()
{
CleanupEditors ();
}
protected override void SubEditorSetup (ReactionEditor editor)
{
editor.reactionsProperty = reactionsProperty;
}
public override void OnInspectorGUI ()
{
serializedObject.Update ();
CheckAndCreateSubEditors(reactionCollection.reactions);
for (int i = 0; i < subEditors.Length; i++)
{
subEditors[i].OnInspectorGUI ();
}
if (reactionCollection.reactions.Length > 0)
{
EditorGUILayout.Space();
EditorGUILayout.Space ();
}
Rect fullWidthRect = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(dropAreaHeight + verticalSpacing));
Rect leftAreaRect = fullWidthRect;
leftAreaRect.y += verticalSpacing * 0.5f;
leftAreaRect.width *= 0.5f;
leftAreaRect.width -= controlSpacing * 0.5f;
leftAreaRect.height = dropAreaHeight;
Rect rightAreaRect = leftAreaRect;
rightAreaRect.x += rightAreaRect.width + controlSpacing;
TypeSelectionGUI (leftAreaRect);
DragAndDropAreaGUI (rightAreaRect);
DraggingAndDropping(rightAreaRect, this);
serializedObject.ApplyModifiedProperties ();
}
private void TypeSelectionGUI (Rect containingRect)
{
Rect topHalf = containingRect;
topHalf.height *= 0.5f;

Rect bottomHalf = topHalf;
bottomHalf.y += bottomHalf.height;
selectedIndex = EditorGUI.Popup(topHalf, selectedIndex, reactionTypeNames);
if (GUI.Button (bottomHalf, "Add Selected Reaction"))
{
Type reactionType = reactionTypes[selectedIndex];
Reaction newReaction = ReactionEditor.CreateReaction (reactionType);
reactionsProperty.AddToObjectArray (newReaction);
}
}
private static void DragAndDropAreaGUI (Rect containingRect)
{
GUIStyle centredStyle = GUI.skin.box;
centredStyle.alignment = TextAnchor.MiddleCenter;
centredStyle.normal.textColor = GUI.skin.button.normal.textColor;
GUI.Box (containingRect, "Drop new Reactions here", centredStyle);
}
private static void DraggingAndDropping (Rect dropArea, ReactionCollectionEditor editor)
{
Event currentEvent = Event.current;
if (!dropArea.Contains (currentEvent.mousePosition))
return;
switch (currentEvent.type)
{
case EventType.DragUpdated:
DragAndDrop.visualMode = IsDragValid () ? DragAndDropVisualMode.Link : DragAndDropVisualMode.Rejected;
currentEvent.Use ();
break;
case EventType.DragPerform:

DragAndDrop.AcceptDrag();

for (int i = 0; i < DragAndDrop.objectReferences.Length; i++)
{
MonoScript script = DragAndDrop.objectReferences[i] as MonoScript;
Type reactionType = script.GetClass();
Reaction newReaction = ReactionEditor.CreateReaction (reactionType);
editor.reactionsProperty.AddToObjectArray (newReaction);
}
currentEvent.Use();
break;
}
}
private static bool IsDragValid ()
{
for (int i = 0; i < DragAndDrop.objectReferences.Length; i++)
{
if (DragAndDrop.objectReferences[i].GetType () != typeof (MonoScript))
return false;

MonoScript script = DragAndDrop.objectReferences[i] as MonoScript;
Type scriptType = script.GetClass ();
if (!scriptType.IsSubclassOf (typeof(Reaction)))
return false;
if (scriptType.IsAbstract)
return false;
}
return true;
}
private void SetReactionNamesArray ()
{
Type reactionType = typeof(Reaction);
Type[] allTypes = reactionType.Assembly.GetTypes();
List<Type> reactionSubTypeList = new List<Type>();
for (int i = 0; i < allTypes.Length; i++)
{
if (allTypes[i].IsSubclassOf(reactionType) && !allTypes[i].IsAbstract)
{
reactionSubTypeList.Add(allTypes[i]);
}
}
reactionTypes = reactionSubTypeList.ToArray();
List<string> reactionTypeNameList = new List<string>();
for (int i = 0; i < reactionTypes.Length; i++)
{
reactionTypeNameList.Add(reactionTypes[i].Name);
}
reactionTypeNames = reactionTypeNameList.ToArray();
}
}


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


34


35


36


37


38


39


40


41


42


43


44


45


46


47


48


49


50


51


52


53


54


55


56


57


58


59


60


61


62


63


64


65


66


67


68


69


70


71


72


73


74


75


76


77


78


79


80


81


82


83


84


85


86


87


88


89


90


91


92


93


94


95


96


97


98


99


100


101


102


103


104


105


106


107


108


109


110


111


112


113


114


115


116


117


118


119


120


121


122


123


124


125


126


127


128


129


130


131


132


133


134


135


136


137


138


139


140


141


142


TextReaction

Expand view

Copy code
using UnityEngine;
public class TextReaction : Reaction
{
public string message;
public Color textColor = Color.white;
public float delay;
private TextManager textManager;
protected override void SpecificInit()
{
textManager = FindObjectOfType<TextManager> ();
}
protected override void ImmediateReaction()
{
textManager.DisplayMessage (message, textColor, delay);
}
}


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


TextReactionEditor

Expand view

Copy code
using UnityEditor;
using UnityEngine;
[CustomEditor(typeof(TextReaction))]public class TextReactionEditor : ReactionEditor
{
private SerializedProperty messageProperty;
private SerializedProperty textColorProperty;
private SerializedProperty delayProperty;
private const float messageGUILines = 3f;
private const float areaWidthOffset = 19f;
private const string textReactionPropMessageName = "message";
private const string textReactionPropTextColorName = "textColor";
private const string textReactionPropDelayName = "delay";
protected override void Init ()
{
messageProperty = serializedObject.FindProperty (textReactionPropMessageName);
textColorProperty = serializedObject.FindProperty (textReactionPropTextColorName);
delayProperty = serializedObject.FindProperty (textReactionPropDelayName);
}
protected override void DrawReaction ()
{
EditorGUILayout.BeginHorizontal ();
EditorGUILayout.LabelField ("Message", GUILayout.Width (EditorGUIUtility.labelWidth - areaWidthOffset));
messageProperty.stringValue = EditorGUILayout.TextArea (messageProperty.stringValue, GUILayout.Height (EditorGUIUtility.singleLineHeight * messageGUILines));
EditorGUILayout.EndHorizontal ();
EditorGUILayout.PropertyField (textColorProperty);
EditorGUILayout.PropertyField (delayProperty);
}
protected override string GetFoldoutLabel ()
{
return "Text Reaction";
}
}


1


2


3


4


5


6


7


8


9


10


11


12


13


14


15


16


17


18


19


20


21


22


23


24


25


26


27


28


29


30


31


32


33


About lbtmicr06

Check Also

Lời dịch bài hát Happy New Year

No more champagneKhông còn rượu sâm panh nữaAnd the fireworks are throughVà pháo hoa cũng …

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *