Skip to content

Commit 4a29e03

Browse files
author
邹杭特
committed
增加.lua后缀的文件导入,改进viewModel的创建
1 parent 92574e8 commit 4a29e03

File tree

77 files changed

+679
-4025
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+679
-4025
lines changed

Assets/BridgeUI/Common/SingleImage/SingleImagePanel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ public class SingleImagePanel : SingleCloseAblePanel
2727
protected override void Awake()
2828
{
2929
base.Awake();
30-
Binder.RegistMember<string>("m_title.text","title");
31-
Binder.RegistMember<Sprite>("m_image.sprite", "sprite");
30+
Binder.RegistValueChange<string>(x=>m_title.text = x,"title");
31+
Binder.RegistValueChange<Sprite>(x=>m_image.sprite = x, "sprite");
3232
}
3333

3434
protected override void HandleData(object data)

Assets/BridgeUI/Common/SpriteInfoBehaiver/SpriteInfoBehaiver.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ protected override void Start()
3333
protected override void PropBindings()
3434
{
3535
base.PropBindings();
36-
Binder.RegistMember<int>(view.SetFontSize, "fontSize");
37-
Binder.RegistMember<Vector3>(SetPosition, "position");
38-
Binder.RegistMember<string>(view.SetTitle, "textInfo");
39-
Binder.RegistMember<Color>(view.SetColor, "fontColor");
40-
Binder.RegistMember<Sprite>(view.SetSprite, "sprite");
36+
Binder.RegistValueChange<int>(view.SetFontSize, "fontSize");
37+
Binder.RegistValueChange<Vector3>(SetPosition, "position");
38+
Binder.RegistValueChange<string>(view.SetTitle, "textInfo");
39+
Binder.RegistValueChange<Color>(view.SetColor, "fontColor");
40+
Binder.RegistValueChange<Sprite>(view.SetSprite, "sprite");
4141
}
4242

4343
public void Update()

Assets/BridgeUI/Common/WaitPopPanel/WaitPopPanel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public class WaitPopPanel : SingleCloseAblePanel
2424
protected override void PropBindings()
2525
{
2626
base.PropBindings();
27-
Binder.RegistMember<bool>("m_close.interactable", "cansaleAble");
28-
Binder.RegistMember<string>("m_title.text", "title");
27+
Binder.RegistValueChange<bool>(x=>m_close.interactable = x, "cansaleAble");
28+
Binder.RegistValueChange<string>(x=>m_title.text = x, "title");
2929
}
3030
}
3131
}

Assets/BridgeUI/Core/Attributes/DefultViewModelAttribute.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ namespace BridgeUI.Attributes
1515
{
1616
public class DefultViewModelAttribute : UnityEngine.PropertyAttribute
1717
{
18-
18+
public bool hide;
19+
public DefultViewModelAttribute(bool hide)
20+
{
21+
this.hide = hide;
22+
}
1923
}
2024

2125
}

Assets/BridgeUI/Core/Attributes/Editor/DefultViewModelDrawer.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,27 @@ namespace BridgeUI.Drawer
2222
[CustomPropertyDrawer(typeof(DefultViewModelAttribute),true)]
2323
public class DefultViewModelDrawer : PropertyDrawer
2424
{
25-
private static ViewModel defultviewModel;
2625
private static GUIContent content = new GUIContent("IViewModel");
26+
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
27+
{
28+
var hide = (attribute as DefultViewModelAttribute).hide;
29+
if(hide && property.objectReferenceValue == null)
30+
{
31+
return 0;
32+
}
33+
else
34+
{
35+
return EditorGUIUtility.singleLineHeight;
36+
}
37+
}
38+
2739
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
2840
{
29-
if (defultviewModel == null)
41+
var hide = (attribute as DefultViewModelAttribute).hide;
42+
43+
if (hide && property.objectReferenceValue == null)
3044
{
31-
defultviewModel = ScriptableObject.CreateInstance<ViewModel>();
45+
return;
3246
}
3347

3448
if (!(property.objectReferenceValue is Binding.IViewModel))
@@ -44,7 +58,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
4458
var target = property.serializedObject.targetObject;
4559
var path = property.propertyPath;
4660

47-
MvvmUtil.CreateNewViewModel((viewModel) =>
61+
MvvmUtil.CreateAssets(typeof(ViewModelContainer), (viewModel) =>
4862
{
4963
var serializeObj = new SerializedObject(target);
5064
var prop = serializeObj.FindProperty(path);
@@ -55,21 +69,7 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5569

5670
rect = new Rect(position.x, position.y, position.width - 60, position.height);
5771

58-
if (property.objectReferenceValue == null)
59-
{
60-
var viewModel = EditorGUI.ObjectField(rect, content, defultviewModel, typeof(ScriptableObject), false) as ScriptableObject;
61-
62-
if (viewModel != defultviewModel && viewModel is IViewModel)
63-
{
64-
property.objectReferenceValue = viewModel;
65-
}
66-
67-
if(viewModel == null)
68-
{
69-
property.objectReferenceValue = null;
70-
}
71-
}
72-
else
72+
if (property.objectReferenceValue != null)
7373
{
7474
EditorGUI.PropertyField(rect, property, content);
7575
}

Assets/BridgeUI/Core/Behaiver/Group/Base/PanelGroupBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public abstract class PanelGroupBase : MonoBehaviour, IPanelGroup
2828
protected List<UICreateInfo> waitCreateQueue = new List<UICreateInfo>();
2929
protected PanelCreateRule createRule;
3030
protected event UnityAction onDestroy;
31+
[BundleLoader("【创建规则】")]
3132
public BundleLoader bundleCreateRule;
3233
public UIBindingController bindingCtrl { get; private set; }
3334
public abstract List<Graph.UIGraph> GraphList { get; }

Assets/BridgeUI/Core/Behaiver/Panels/PanelBase.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public virtual Binding.PropertyBinder Binder
4040
return _binder;
4141
}
4242
}
43-
[SerializeField, Attributes.DefultViewModel]
43+
[SerializeField, Attributes.DefultViewModel(true)]
4444
private ScriptableObject _viewModel;
4545
private IViewModel _defultViewModel;
4646
protected IViewModel defultViewModel
@@ -67,6 +67,20 @@ public IViewModel ViewModel
6767
}
6868
}
6969

70+
#if UNITY_EDITOR
71+
72+
[ContextMenu("加载ViewModel")]
73+
private void LoadDefultViewModel()
74+
{
75+
_viewModel = ScriptableObject.CreateInstance<ViewModel>();
76+
}
77+
[ContextMenu("清除ViewModel")]
78+
private void ClearDefultViewModel()
79+
{
80+
_viewModel =null;
81+
}
82+
#endif
83+
7084
protected override void Awake()
7185
{
7286
base.Awake();

Assets/BridgeUI/Core/Binding/Editor/ViewModelContainerDrawer.cs

Lines changed: 28 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -14,156 +14,77 @@ public class ViewModelContainerDrawer : Editor
1414
{
1515
private GUIContent[] options;
1616
private Type[] viewModelTypes;
17-
private SerializedProperty prop_instences;
18-
private ReorderableList instencesList;
19-
private List<string> ignoreProps = new List<string> { "m_Script" };
17+
private SerializedProperty prop_instence;
2018
public const float middleButtonWidth = 45f;
21-
19+
private Editor editor;
20+
2221
private void OnEnable()
2322
{
24-
InitPropertys();
23+
prop_instence = serializedObject.FindProperty("instence");
2524
InitTypeOptions();
26-
InitInstenceList();
25+
26+
if (prop_instence.objectReferenceValue != null)
27+
{
28+
editor = Editor.CreateEditor(prop_instence.objectReferenceValue);
29+
}
2730
}
2831

2932
public override void OnInspectorGUI()
3033
{
31-
base.OnInspectorGUI();
3234
serializedObject.Update();
33-
TryDrawViewModelList();
35+
DrawInstence();
3436
serializedObject.ApplyModifiedProperties();
3537
}
3638

37-
private void InitInstenceList()
38-
{
39-
instencesList = new ReorderableList(prop_instences.serializedObject, prop_instences);
40-
instencesList.onAddCallback = OnAddElement;
41-
instencesList.onRemoveCallback = OnRemoveElement;
42-
instencesList.drawHeaderCallback = OnDrawHeader;
43-
instencesList.drawElementCallback = DrawElementCallBack;
44-
instencesList.elementHeightCallback = ElementHeightCallback;
45-
}
46-
47-
private void OnDrawHeader(Rect rect)
48-
{
49-
EditorGUI.LabelField(rect, "视图模型列表(--优先绑定第一个ViewModel--)");
50-
}
51-
52-
protected float ElementHeightCallback(int index)
53-
{
54-
var prop = prop_instences.GetArrayElementAtIndex(index);
55-
var height = EditorGUIUtility.singleLineHeight + BridgeEditorUtility.padding * 2;
56-
if (prop.objectReferenceValue != null && prop.isExpanded)
57-
{
58-
var se = BridgeUI.Drawer.BridgeEditorUtility. CreateCachedSerializedObject(prop.objectReferenceValue);
59-
height += BridgeEditorUtility.GetSerializedObjectHeight(se, ignoreProps) + BridgeEditorUtility.padding * 2;
60-
}
61-
return height;
62-
}
63-
64-
protected void DrawElementCallBack(Rect rect, int index, bool isActive, bool isFocused)
39+
private void DrawInstence()
6540
{
66-
rect = BridgeUI.Drawer.BridgeEditorUtility.DrawBoxRect(rect, index.ToString());
67-
var prop = prop_instences.GetArrayElementAtIndex(index);
68-
var content = prop.objectReferenceValue == null ? new GUIContent("Null") : new GUIContent(prop.objectReferenceValue.name);
69-
var bgColor = index == 0 ? Color.green:Color.red;
70-
71-
var btnRect = new Rect(rect.x, rect.y, rect.width - middleButtonWidth, EditorGUIUtility.singleLineHeight);
72-
var objRect = new Rect(rect.x + rect.width - middleButtonWidth, rect.y, middleButtonWidth, EditorGUIUtility.singleLineHeight);
73-
74-
var defultColor = GUI.backgroundColor;
75-
GUI.backgroundColor = bgColor;
76-
if (GUI.Button(btnRect, content, EditorStyles.toolbarDropDown))
77-
{
78-
prop.isExpanded = !prop.isExpanded;
41+
if (GUILayout.Button("更改ViewModel类型",EditorStyles.toolbarButton)){
42+
OnUpdateElement();
7943
}
80-
GUI.backgroundColor = defultColor;
8144

82-
if (prop.objectReferenceValue != null)
45+
if (editor != null && editor.target && prop_instence.objectReferenceValue != null)
8346
{
84-
if (GUI.Button(objRect, "", EditorStyles.objectFieldMiniThumb))
85-
{
86-
EditorGUIUtility.PingObject(prop.objectReferenceValue);
87-
}
88-
}
89-
else
90-
{
91-
EditorGUI.LabelField(objRect, "Lost!!!");
92-
}
93-
94-
if (prop.isExpanded && prop.objectReferenceValue != null)
95-
{
96-
DrawObjectDetail(prop.objectReferenceValue, rect);
47+
editor.serializedObject.Update();
48+
editor.OnInspectorGUI();
49+
editor.serializedObject.ApplyModifiedProperties();
9750
}
9851
}
99-
private void OnRemoveElement(ReorderableList list)
100-
{
101-
if(list.index >=0 && list.index < list.count)
102-
{
103-
var prop = prop_instences.GetArrayElementAtIndex(list.index);
10452

105-
if (prop.objectReferenceValue)
106-
{
107-
DestroyImmediate(prop.objectReferenceValue,true);
108-
Debug.Log("删除:"+ prop.objectReferenceValue);
109-
}
11053

111-
prop_instences.DeleteArrayElementAtIndex(list.index);
112-
prop_instences.serializedObject.ApplyModifiedProperties();
113-
}
114-
}
115-
116-
private void OnAddElement(ReorderableList list)
54+
private void OnUpdateElement()
11755
{
11856
EditorUtility.DisplayCustomMenu(new Rect(Event.current.mousePosition, Vector2.zero), options, -1, (x, y, z) =>
11957
{
58+
if (prop_instence.objectReferenceValue != null)
59+
DestroyImmediate(prop_instence.objectReferenceValue, true);
60+
12061
var type = viewModelTypes[z];
12162
var instence = GetAssetByType(type);
122-
prop_instences.InsertArrayElementAtIndex(0);
123-
var prop = prop_instences.GetArrayElementAtIndex(0);
124-
prop.objectReferenceValue = instence;
125-
prop_instences.serializedObject.ApplyModifiedProperties();
63+
prop_instence.objectReferenceValue = instence;
64+
serializedObject.ApplyModifiedProperties();
65+
if (prop_instence.objectReferenceValue != null){
66+
editor = Editor.CreateEditor(prop_instence.objectReferenceValue);
67+
}
68+
AssetDatabase.Refresh();
69+
12670
}, null);
12771
}
12872

12973
private ScriptableObject GetAssetByType(Type type)
13074
{
13175
var path = AssetDatabase.GetAssetPath(target);
132-
//var assets = AssetDatabase.LoadAllAssetsAtPath(path);
13376
var instence = ScriptableObject.CreateInstance(type);
13477
instence.name = type.Name;
13578
instence.hideFlags = HideFlags.HideInHierarchy;
13679
AssetDatabase.AddObjectToAsset(instence, path);
13780
return instence as ScriptableObject;
13881
}
13982

140-
private void TryDrawViewModelList()
141-
{
142-
instencesList.DoLayoutList();
143-
}
144-
145-
private void InitPropertys()
146-
{
147-
prop_instences = serializedObject.FindProperty("instences");
148-
}
149-
15083
private void InitTypeOptions()
15184
{
15285
viewModelTypes = BridgeUI.Drawer.MvvmUtil.GetSubInstenceTypes(typeof(ViewModel)).ToArray();
15386
options = viewModelTypes.Select(x => new GUIContent(x.FullName)).ToArray();
15487
}
15588

156-
protected void DrawObjectDetail(UnityEngine.Object obj, Rect rect)
157-
{
158-
if (obj != null)
159-
{
160-
var serializedObj = BridgeEditorUtility.CreateCachedSerializedObject(obj);
161-
rect.y += EditorGUIUtility.singleLineHeight + 5;
162-
serializedObj.Update();
163-
BridgeEditorUtility.DrawChildInContent(serializedObj.GetIterator(), rect, ignoreProps, null, 1);
164-
serializedObj.ApplyModifiedProperties();
165-
}
166-
}
167-
16889
}
16990
}

0 commit comments

Comments
 (0)