Skip to content
This repository was archived by the owner on May 9, 2025. It is now read-only.

Commit 9ed61e8

Browse files
Added readonly variables
1 parent deb3b5f commit 9ed61e8

File tree

2 files changed

+57
-6
lines changed

2 files changed

+57
-6
lines changed

Assets/SO Architecture/Editor/Inspectors/BaseVariableEditor.cs

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using UnityEngine;
55
using UnityEditor;
6+
using UnityEditor.AnimatedValues;
67

78
[CustomEditor(typeof(BaseVariable<>), true)]
89
public class BaseVariableEditor : Editor
@@ -11,16 +12,27 @@ public class BaseVariableEditor : Editor
1112

1213
private SerializedProperty _valueProperty;
1314
private SerializedProperty _developerDescription;
15+
private SerializedProperty _readOnly;
16+
private SerializedProperty _raiseWarning;
17+
private AnimBool _raiseWarningAnimation;
18+
19+
private const string READONLY_TOOLTIP = "Should this value be changable during runtime? Will still be editable in the inspector regardless";
1420

1521
private void OnEnable()
1622
{
1723
_valueProperty = serializedObject.FindProperty("_value");
1824
_developerDescription = serializedObject.FindProperty("DeveloperDescription");
25+
_readOnly = serializedObject.FindProperty("_readOnly");
26+
_raiseWarning = serializedObject.FindProperty("_raiseWarning");
27+
28+
_raiseWarningAnimation = new AnimBool(_readOnly.boolValue);
29+
_raiseWarningAnimation.valueChanged.AddListener(Repaint);
1930
}
2031
public override void OnInspectorGUI()
2132
{
2233
serializedObject.Update();
23-
34+
35+
// Value.
2436
if (SOArchitecture_EditorUtility.HasPropertyDrawer(Target.Type))
2537
{
2638
//Unity doesn't like it when you have scene objects on assets,
@@ -46,7 +58,21 @@ public override void OnInspectorGUI()
4658
EditorGUILayout.LabelField(new GUIContent(labelContent, labelContent));
4759
}
4860

61+
// Readonly.
62+
EditorGUILayout.PropertyField(_readOnly, new GUIContent("Read Only", READONLY_TOOLTIP));
63+
64+
_raiseWarningAnimation.target = _readOnly.boolValue;
65+
using (var fadeGroup = new EditorGUILayout.FadeGroupScope(_raiseWarningAnimation.faded))
66+
{
67+
if(fadeGroup.visible)
68+
{
69+
EditorGUI.indentLevel++;
70+
EditorGUILayout.PropertyField(_raiseWarning);
71+
EditorGUI.indentLevel--;
72+
}
73+
}
4974

75+
// Developer Description.
5076
EditorGUILayout.PropertyField(_developerDescription);
5177
}
5278
}

Assets/SO Architecture/Variables/BaseVariable.cs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,50 @@ public interface IBaseVariable
66
}
77
public abstract class BaseVariable : SOArchitectureBaseObject, IBaseVariable
88
{
9+
public abstract bool ReadOnly { get; }
910
public abstract System.Type Type { get; }
1011
public abstract object BaseValue { get; set; }
1112
}
1213
public abstract class BaseVariable<T> : BaseVariable
1314
{
14-
public T Value { get { return _value; } set { _value = value; } }
15+
public T Value { get { return _value; } set { SetValue(value); } }
16+
public override bool ReadOnly { get { return _readOnly; } }
1517
public override System.Type Type { get { return typeof(T); } }
16-
public override object BaseValue { get { return _value; } set { _value = (T)value; } }
18+
public override object BaseValue { get { return _value; } set { SetValue((T)value); } }
1719

1820
[SerializeField]
1921
protected T _value;
20-
22+
[SerializeField]
23+
private bool _readOnly = false;
24+
[SerializeField]
25+
private bool _raiseWarning = true;
26+
2127
public void SetValue(T value)
2228
{
23-
Value = value;
29+
if (_readOnly)
30+
{
31+
RaiseReadonlyWarning();
32+
return;
33+
}
34+
35+
_value = value;
2436
}
2537
public void SetValue(BaseVariable<T> value)
2638
{
27-
Value = value.Value;
39+
if (_readOnly)
40+
{
41+
RaiseReadonlyWarning();
42+
return;
43+
}
44+
45+
_value = value.Value;
46+
}
47+
private void RaiseReadonlyWarning()
48+
{
49+
if (!_readOnly || !_raiseWarning)
50+
return;
51+
52+
Debug.LogWarning("Tried to set value on " + name + ", but value is readonly!", this);
2853
}
2954

3055
public override string ToString()

0 commit comments

Comments
 (0)