- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 Unity 2017.2 中开发一款小型 ARPG。
我已经尝试为我的游戏的 AbilityBluePrint 类实现自定义编辑器。
基本上,AbilityBluePrints 包含在运行时生成 Ability 所需的所有信息。包括一个 Effect[] ScritpableObjects 数组,当使用该能力时会被触发。
我目前已实现和工作所需的一切,但我认为由于以下原因,创建能力会非常乏味。
假设我有一个效果类 DamagePlusX:作为伤害修正值的效果。如果我希望此效果对两种不同的能力具有不同的修饰符值,那么我将必须在我的 Asset 目录中创建它的两个实例,并手动将每个实例分配给相应能力的 Effect[] 数组。我担心我最终会有很多很多效果实例,每个实例基本上都有一些不同的整数和 float 。
因此,我想我会使用一个自定义检查器,有点像 the Adventure Tutorial 中的检查器。来自 Unity。
想法基本上是创建 AbilityBluePrint 的实例,然后使用自定义检查器能够在 Effects[] 数组中动态实例化效果,并能够直接在 AbilityBluePrint 检查器中编辑每个效果的属性。
基本上我想要一些类似的东西(为可怜的 photoshop 道歉):
我试图转换教程中的脚本以满足我的需要,但从昨天开始我一直遇到同样的错误:
NullReferenceException: Object reference not set to an instance of an object
AbilityBluePrintEditor.SubEditorSetup (.EffectEditor editor) (at Assets/Scripts/Editor/AbilityBluePrintEditor.cs:90)
EditorWithSubEditors`2[TEditor,TTarget].CheckAndCreateSubEditors (.TTarget[] subEditorTargets) (at Assets/Scripts/Editor/EditorWithSubEditors.cs:33)
我已经尝试了很多事情,我想知道我正在尝试做的事情是否可以用可编写脚本的对象来实现。在原始教程中,与我的 BluePrintAbility 等效的是 Monobehaviour。
我的代码如下:
我的 BluePrintAbility 类:
[CreateAssetMenu(fileName = "New Ability BluePrint", menuName = "Ability BluePrint")]
public class AbilityBluePrint : ScriptableObject {
public Effect[] effects = new Effect[0];
public string description;
}
我的效果类:
public abstract class Effect : ScriptableObject {
}
我的 DamagePlusX 效果类:
[CreateAssetMenu(fileName = "DamagePlusX",menuName = "Effects/DamagePlusX")]
public class DamagePlusX : Effect
{
[SerializeField]
int modifier;
public void ApplyModifier(){ // some logic}
}
现在是编辑们(为冗长的样本道歉,但我现在不知道错误在哪里,尽管我已经削减了主要类):
这是教程中的基础编辑器,我的错误来自于:
// This class acts as a base class for Editors that have Editors
// nested within them. For example, the InteractableEditor has
// an array of ConditionCollectionEditors.
// It's generic types represent the type of Editor array that are
// nested within this Editor and the target type of those Editors.
public abstract class EditorWithSubEditors<TEditor, TTarget> : Editor
where TEditor : Editor
where TTarget : Object
{
protected TEditor[] subEditors; // Array of Editors nested within this Editor.
// This should be called in OnEnable and at the start of OnInspectorGUI.
protected void CheckAndCreateSubEditors (TTarget[] subEditorTargets)
{
// If there are the correct number of subEditors then do nothing.
if (subEditors != null && subEditors.Length == subEditorTargets.Length)
return;
// Otherwise get rid of the editors.
CleanupEditors ();
// Create an array of the subEditor type that is the right length for the targets.
subEditors = new TEditor[subEditorTargets.Length];
// Populate the array and setup each Editor.
for (int i = 0; i < subEditors.Length; i++)
{
subEditors[i] = CreateEditor (subEditorTargets[i]) as TEditor;
SubEditorSetup (subEditors[i]); // ERROR comes inside this function HERE !!!!
}
}
// This should be called in OnDisable.
protected void CleanupEditors ()
{
// If there are no subEditors do nothing.
if (subEditors == null)
return;
// Otherwise destroy all the subEditors.
for (int i = 0; i < subEditors.Length; i++)
{
DestroyImmediate (subEditors[i]);
}
// Null the array so it's GCed.
subEditors = null;
}
// This must be overridden to provide any setup the subEditor needs when it is first created.
protected abstract void SubEditorSetup (TEditor editor);
[CustomEditor(typeof(AbilityBluePrint)), CanEditMultipleObjects]
public class AbilityBluePrintEditor : EditorWithSubEditors<EffectEditor, Effect>
{
private AbilityBluePrint blueprint; // Reference to the target.
private SerializedProperty effectsProperty; //represents the array of effects.
private Type[] effectTypes; // All the non-abstract types which inherit from Effect. This is used for adding new Effects.
private string[] effectTypeNames; // The names of all appropriate Effect types.
private int selectedIndex; // The index of the currently selected Effect type.
private const float dropAreaHeight = 50f; // Height in pixels of the area for dropping scripts.
private const float controlSpacing = 5f; // Width in pixels between the popup type selection and drop area.
private const string effectsPropName = "effects"; // Name of the field for the array of Effects.
private readonly float verticalSpacing = EditorGUIUtility.standardVerticalSpacing;
// Caching the vertical spacing between GUI elements.
private void OnEnable()
{
// Cache the target.
blueprint = (AbilityBluePrint)target;
// Cache the SerializedProperty
effectsProperty = serializedObject.FindProperty(effectsPropName);
// If new editors for Effects are required, create them.
CheckAndCreateSubEditors(blueprint.effects);
// Set the array of types and type names of subtypes of Reaction.
SetEffectNamesArray();
}
public override void OnInspectorGUI()
{
// Pull all the information from the target into the serializedObject.
serializedObject.Update();
// If new editors for Reactions are required, create them.
CheckAndCreateSubEditors(blueprint.effects);
DrawDefaultInspector();
// Display all the Effects.
for (int i = 0; i < subEditors.Length; i++)
{
if (subEditors[i] != null)
{
subEditors[i].OnInspectorGUI();
}
}
// If there are Effects, add a space.
if (blueprint.effects.Length > 0)
{
EditorGUILayout.Space();
EditorGUILayout.Space();
}
//Shows the effect selection GUI
SelectionGUI();
if (GUILayout.Button("Add Effect"))
{
}
// Push data back from the serializedObject to the target.
serializedObject.ApplyModifiedProperties();
}
private void OnDisable()
{
// Destroy all the subeditors.
CleanupEditors();
}
// This is called immediately after each ReactionEditor is created.
protected override void SubEditorSetup(EffectEditor editor)
{
// Make sure the ReactionEditors have a reference to the array that contains their targets.
editor.effectsProperty = effectsProperty; //ERROR IS HERE !!!
}
private void SetEffectNamesArray()
{
// Store the Effect type.
Type effectType = typeof(Effect);
// Get all the types that are in the same Assembly (all the runtime scripts) as the Effect type.
Type[] allTypes = effectType.Assembly.GetTypes();
// Create an empty list to store all the types that are subtypes of Effect.
List<Type> effectSubTypeList = new List<Type>();
// Go through all the types in the Assembly...
for (int i = 0; i < allTypes.Length; i++)
{
// ... and if they are a non-abstract subclass of Effect then add them to the list.
if (allTypes[i].IsSubclassOf(effectType) && !allTypes[i].IsAbstract)
{
effectSubTypeList.Add(allTypes[i]);
}
}
// Convert the list to an array and store it.
effectTypes = effectSubTypeList.ToArray();
// Create an empty list of strings to store the names of the Effect types.
List<string> reactionTypeNameList = new List<string>();
// Go through all the Effect types and add their names to the list.
for (int i = 0; i < effectTypes.Length; i++)
{
reactionTypeNameList.Add(effectTypes[i].Name);
}
// Convert the list to an array and store it.
effectTypeNames = reactionTypeNameList.ToArray();
}
private void SelectionGUI()
{
// Create a Rect for the full width of the inspector with enough height for the drop area.
Rect fullWidthRect = GUILayoutUtility.GetRect(GUIContent.none, GUIStyle.none, GUILayout.Height(dropAreaHeight + verticalSpacing));
// Create a Rect for the left GUI controls.
Rect leftAreaRect = fullWidthRect;
// It should be in half a space from the top.
leftAreaRect.y += verticalSpacing * 0.5f;
// The width should be slightly less than half the width of the inspector.
leftAreaRect.width *= 0.5f;
leftAreaRect.width -= controlSpacing * 0.5f;
// The height should be the same as the drop area.
leftAreaRect.height = dropAreaHeight;
// Create a Rect for the right GUI controls that is the same as the left Rect except...
Rect rightAreaRect = leftAreaRect;
// ... it should be on the right.
rightAreaRect.x += rightAreaRect.width + controlSpacing;
// Display the GUI for the type popup and button on the left.
TypeSelectionGUI(leftAreaRect);
}
private void TypeSelectionGUI(Rect containingRect)
{
// Create Rects for the top and bottom half.
Rect topHalf = containingRect;
topHalf.height *= 0.5f;
Rect bottomHalf = topHalf;
bottomHalf.y += bottomHalf.height;
// Display a popup in the top half showing all the reaction types.
selectedIndex = EditorGUI.Popup(topHalf, selectedIndex, effectTypeNames);
// Display a button in the bottom half that if clicked...
if (GUI.Button(bottomHalf, "Add Selected Effect"))
{
// ... finds the type selected by the popup, creates an appropriate reaction and adds it to the array.
Debug.Log(effectTypes[selectedIndex]);
Type effectType = effectTypes[selectedIndex];
Effect newEffect = EffectEditor.CreateEffect(effectType);
Debug.Log(newEffect);
effectsProperty.AddToObjectArray(newEffect);
}
}
}
public abstract class EffectEditor : Editor
{
public bool showEffect = true; // Is the effect editor expanded?
public SerializedProperty effectsProperty; // Represents the SerializedProperty of the array the target belongs to.
private Effect effect; // The target Reaction.
private const float buttonWidth = 30f; // Width in pixels of the button to remove this Reaction from the ReactionCollection array.
private void OnEnable()
{
// Cache the target reference.
effect = (Effect)target;
// Call an initialisation method for inheriting classes.
Init();
}
// This function should be overridden by inheriting classes that need initialisation.
protected virtual void Init() { }
public override void OnInspectorGUI()
{
Debug.Log("attempt to draw effect inspector");
// Pull data from the target into the serializedObject.
serializedObject.Update();
EditorGUILayout.BeginVertical(GUI.skin.box);
EditorGUI.indentLevel++;
DrawDefaultInspector();
EditorGUI.indentLevel--;
EditorGUILayout.EndVertical();
// Push data back from the serializedObject to the target.
serializedObject.ApplyModifiedProperties();
}
public static Effect CreateEffect(Type effectType)
{
// Create a reaction of a given type.
return (Effect) ScriptableObject.CreateInstance(effectType);
}
}
[CustomEditor(typeof(DamagePlusXEditor))]
public class DamagePlusXEditor : EffectEditor {}
最佳答案
不确定它是否对您的具体情况有帮助,但我很幸运将数据存储在纯 C# 类中,然后将这些数据的数组嵌套在 ScriptableObject 中,并且两者的自定义编辑器一起工作.
例如这个纯数据类(也由其他相当简单的纯类组成):
[System.Serializable]
public class Buff
{
public CharacterAttribute attribute;
public CalculationType calculationType;
public BuffDuration buffDuration;
public bool effectBool;
public int effectInt;
public float effectFloat;
}
编辑器如下:
[CustomPropertyDrawer (typeof (Buff))]
public class BuffDrawer : PropertyDrawer
{
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label)
...
然后是包含这些“Buff”对象数组的 SO:
[CreateAssetMenu (fileName = "New Buff", menuName = "Data/Buff")]
public class BuffData : ScriptableObject
{
public new string name;
public string description;
public Texture2D icon;
public Buff [] attributeBuffs;
}
最后是 SO 的编辑器(请参阅靠近底部的 PropertyField):
using UnityEngine;
using UnityEditor;
[CustomEditor (typeof (BuffData))]
public class BuffDataEditor : Editor
{
private const int DescriptionWidthPadding = 35;
private const float DescriptionHeightPadding = 1.25f;
private const string AttributesHelpText =
"Choose which attributes are to be affected by this buff and by how much.\n" +
"Note: the calculation type should match the attribute's implementation.";
private SerializedProperty nameProperty;
private SerializedProperty descriptionProperty;
private SerializedProperty iconProperty;
private SerializedProperty attributeBuffsProperty;
private void OnEnable ()
{
nameProperty = serializedObject.FindProperty ("name");
descriptionProperty = serializedObject.FindProperty ("description");
iconProperty = serializedObject.FindProperty ("icon");
attributeBuffsProperty = serializedObject.FindProperty ("attributeBuffs");
}
public override void OnInspectorGUI()
{
serializedObject.Update ();
nameProperty.stringValue = EditorGUILayout.TextField ("Name", nameProperty.stringValue);
EditorGUILayout.LabelField ("Description:");
GUIStyle descriptionStyle = new GUIStyle (EditorStyles.textArea)
{
wordWrap = true,
padding = new RectOffset (6, 6, 6, 6),
fixedWidth = Screen.width - DescriptionWidthPadding
};
descriptionStyle.fixedHeight = descriptionStyle.CalcHeight (new GUIContent (descriptionProperty.stringValue), Screen.width) * DescriptionHeightPadding;
EditorGUI.indentLevel++;
descriptionProperty.stringValue = EditorGUILayout.TextArea (descriptionProperty.stringValue, descriptionStyle);
EditorGUI.indentLevel--;
EditorGUILayout.Space ();
iconProperty.objectReferenceValue = (Texture2D) EditorGUILayout.ObjectField ("Icon", iconProperty.objectReferenceValue, typeof (Texture2D), false);
EditorGUILayout.Space ();
EditorGUILayout.HelpBox (AttributesHelpText, MessageType.Info);
EditorGUILayout.PropertyField (attributeBuffsProperty, true);
serializedObject.ApplyModifiedProperties();
}
}
所有这些导致:
无论如何,希望这个例子能给您一些可能对您有所帮助的想法。
关于c# - 带有子检查器的 Unity 自定义检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47336954/
我想使用 li 和 ul 制作一个多级下拉列表,以便显示我博客中按年和月排序的所有文章。我希望我的下拉菜单看起来像 Google Blogspot 下拉菜单: 这是我的 CSS 和 HTML 代码 u
我在 Win 7 64 机器上将 CodeBlocks 与 gcc 4.7.2 和 gmp 5.0.5 结合使用。开始使用 gmpxx 后,我看到一个奇怪的段错误,它不会出现在 +、- 等运算符中,但
我正在使用 tern 为使用 CodeMirror 运行的窗口提供一些增强的智能感知,它工作正常,但我遇到了一个问题,我想添加一些自定义“types”,可以这么说,这样下拉列表中它们旁边就有图标了。我
我正在尝试让我的 PC 成为 Android 2.3.4 设备的 USB 主机,以便能够在不需要实际“附件”的情况下开发 API。为此,我需要将 PC 设置为 USB 主机和“设备”(在我的例子中是运
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
我在设置服务器方面几乎是个新手,但遇到了一个问题。我有一个 Ubuntu 16.04 VPS 并安装了 Apache2 和 Tomcat7。我正在为 SSL 使用 LetsEncrypt 和 Cert
我在一个基于谷歌地图的项目上工作了超过 6 个月。我使用的是 Google Maps API V1 及其开发人员 API key 。当我尝试发布应用程序时,我了解到 Google API V1 已被弃
我是 Python 的新手,所以如果我对一些简单的事情感到困惑,请原谅。 我有一个这样的对象: class myObject(object): def __init__(self):
这个问题已经有答案了: How can I access object properties containing special characters? (2 个回答) 已关闭 9 年前。 我正在尝
我有下面的 CSS。我想要的是一种流体/液体(因为缺乏正确的术语)css。我正在为移动设备开发,当我改变模式时 从纵向 View 到陆地 View ,我希望它流畅。现在的图像 在陆地 View 中效
我正在尝试使用可以接受参数的缓存属性装饰器。 我查看了这个实现:http://www.daniweb.com/software-development/python/code/217241/a-cac
这个问题在这里已经有了答案: Understanding slicing (36 个答案) 关闭 6 年前。 以a = [1,2,3,4,5]为例。根据我的直觉,我认为 a[::-1] 与 a[0:
mysqldump -t -u root -p mytestdb mytable --where=datetime LIKE '2014-09%' 这就是我正在做的事情,它会返回: mysqldum
我正在制作销售税计算器,除了总支付金额部分外,其他一切都正常。在我的程序中,我希望能够输入一个数字并获得该项目的税额我还希望能够获得支付的总金额,包括交易中的税金。到目前为止,我编写的代码完成了所有这
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许在 Stack Overflow 上提出有关通用计算硬件和软件的问题。您可以编辑问题,使其成为
我是否必须进行任何额外的设置才能让 apache-airflow 在任务失败时向我发送电子邮件。我的配置文件中有以下内容(与默认值保持不变): [email] email_backend = airf
这个问题在这里已经有了答案: What does the $ symbol do in VBA? (5 个回答) 3年前关闭。 使用返回字符串(如 Left)的内置函数有什么区别吗?或使用与 $ 相同
我有一个用VB6编写的应用程序,我需要使用一个用.NET编写的库。有什么方法可以在我的应用程序上使用该库吗? 谢谢 最佳答案 这取决于。您可以控制.NET库吗? 如果是这样,则可以修改您的库,以便可以
当我创建一个以 ^ 开头的类方法时,我尝试调用它,它给了我一个错误。 class C { method ^test () { "Hi" } } dd C.new.test; Too m
我已经使用 bower 安装了 angularjs 和 materialjs。 凉亭安装 Angular Material 并将“ngMaterial”注入(inject)我的应用程序,但出现此错误。
我是一名优秀的程序员,十分优秀!