浏览代码

[Sample] Update Drag&Drop Samples

/main
iizzaya 5 年前
当前提交
fd2c107b
共有 8 个文件被更改,包括 541 次插入286 次删除
  1. 2
      Samples/UIWidgetSample/DragNDrop/CustomInspectorSample.cs.meta
  2. 8
      Samples/UIWidgetSample/DragNDrop.meta
  3. 350
      Samples/UIWidgetSample/DragNDrop/CustomInspectorSample.cs
  4. 171
      Samples/UIWidgetSample/DragNDrop/UnityObjectDetectorSample.cs
  5. 11
      Samples/UIWidgetSample/DragNDrop/UnityObjectDetectorSample.cs.meta
  6. 285
      Samples/UIWidgetSample/DragNDropSample.cs
  7. 0
      /Samples/UIWidgetSample/DragNDrop/CustomInspectorSample.cs.meta

2
Samples/UIWidgetSample/DragNDrop/CustomInspectorSample.cs.meta


fileFormatVersion: 2
guid: b6a3da7adaad445f29cdfa60ed347979
guid: 5978da8ac1ccd4638bfe3d8425443807
MonoImporter:
externalObjects: {}
serializedVersion: 2

8
Samples/UIWidgetSample/DragNDrop.meta


fileFormatVersion: 2
guid: 2f04d8a9cd0bd4db7b66310e37409f4b
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

350
Samples/UIWidgetSample/DragNDrop/CustomInspectorSample.cs


using System.Collections.Generic;
using Unity.UIWidgets.editor;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.service;
using Unity.UIWidgets.ui;
using Unity.UIWidgets.widgets;
using UnityEditor;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
using Transform = UnityEngine.Transform;
namespace UIWidgetsSample.DragNDrop {
public class CustomInspectorSample : UIWidgetsEditorWindow {
[MenuItem("UIWidgetsTests/Drag&Drop/Custom Inspector")]
public static void ShowEditorWindow() {
var window = GetWindow<CustomInspectorSample>();
window.titleContent.text = "Custom Inspector Sample";
}
protected override void OnEnable() {
FontManager.instance.addFont(Resources.Load<Font>("MaterialIcons-Regular"), "Material Icons");
FontManager.instance.addFont(Resources.Load<Font>("GalleryIcons"), "GalleryIcons");
base.OnEnable();
}
protected override Widget createWidget() {
Debug.Log("[ WIDGET RECREATED ]");
return new MaterialApp(
home: new CustomInspectorSampleWidget(),
darkTheme: new ThemeData(primaryColor: Colors.black26)
);
}
}
public class CustomInspectorSampleWidget : StatefulWidget {
public CustomInspectorSampleWidget(Key key = null) : base(key) {
}
public override State createState() {
return new CustomInspectorSampleWidgetState();
}
}
public class CustomInspectorSampleWidgetState : State<CustomInspectorSampleWidget> {
GameObject objectRef;
Transform transformRef;
TextEditingController textController = new TextEditingController();
public override void initState() {
this.textController.addListener(() => {
var text = this.textController.text.ToLower();
this.textController.value = this.textController.value.copyWith(
text: text,
selection: new TextSelection(baseOffset: text.Length, extentOffset: text.Length),
composing: TextRange.empty
);
});
base.initState();
}
enum ETransfrom {
Position,
Rotation,
Scale
}
int oldCursorPosition = 0;
Widget getCardRow(ETransfrom type, bool hasRef) {
var xValue = hasRef
? type == ETransfrom.Position
? this.transformRef.position.x.ToString()
: type == ETransfrom.Rotation
? this.transformRef.localEulerAngles.x.ToString()
: this.transformRef.localScale.x.ToString()
: "";
var xValueController = TextEditingController.fromValue(
new TextEditingValue(xValue, TextSelection.collapsed(this.oldCursorPosition))
);
var yValue = hasRef
? type == ETransfrom.Position
? this.transformRef.position.y.ToString()
: type == ETransfrom.Scale
? this.transformRef.localEulerAngles.y.ToString()
: this.transformRef.localScale.y.ToString()
: "";
var yValueController = TextEditingController.fromValue(
new TextEditingValue(yValue, TextSelection.collapsed(this.oldCursorPosition))
);
var zValue = hasRef
? type == ETransfrom.Position
? this.transformRef.position.z.ToString()
: type == ETransfrom.Rotation
? this.transformRef.localEulerAngles.z.ToString()
: this.transformRef.localScale.z.ToString()
: "";
var zValueController = TextEditingController.fromValue(
new TextEditingValue(zValue, TextSelection.collapsed(this.oldCursorPosition))
);
return new Column(
children: new List<Widget> {
new Container(
padding: EdgeInsets.symmetric(vertical: 8f),
child: new Align(
alignment: Alignment.centerLeft,
child: new Text(
type == ETransfrom.Position ? "Position" :
type == ETransfrom.Rotation ? "Rotation" : "Scale",
style: new TextStyle(fontSize: 16.0f)
)
)
),
new Row(
children: new List<Widget> {
new Flexible(
flex: 8,
child: new Container(
decoration: new BoxDecoration(
color: new Color(0xfff5f5f5)),
child: new TextField(
decoration: new InputDecoration(
border: new UnderlineInputBorder(),
contentPadding:
EdgeInsets.symmetric(
horizontal: 10f, vertical: 5f),
labelText: "X"
),
controller: xValueController,
onChanged: hasRef
? (str) => {
this.setState(() => {
float result = 0;
float.TryParse(str, out result);
this.oldCursorPosition = xValueController.selection.startPos.offset;
switch (type) {
case ETransfrom.Position:
var newPos = this.transformRef.position;
newPos.x = result;
this.transformRef.position = newPos;
break;
case ETransfrom.Rotation:
var newRot = this.transformRef.localEulerAngles;
newRot.x = result;
this.transformRef.localEulerAngles = newRot;
break;
case ETransfrom.Scale:
var newScale = this.transformRef.localScale;
newScale.x = result;
this.transformRef.localScale = newScale;
break;
}
});
}
: (ValueChanged<string>) null
)
)),
new Flexible(
child: new Container()
),
new Flexible(
flex: 8,
child: new Container(
decoration: new BoxDecoration(
color: new Color(0xfff5f5f5)),
child: new TextField(
decoration: new InputDecoration(
border: new UnderlineInputBorder(),
contentPadding:
EdgeInsets.symmetric(
horizontal: 10f, vertical: 5f),
labelText: "Y"
),
controller: yValueController,
onChanged: hasRef
? (str) => {
this.setState(() => {
float result = 0;
float.TryParse(str, out result);
this.oldCursorPosition = yValueController.selection.startPos.offset;
switch (type) {
case ETransfrom.Position:
var newPos = this.transformRef.position;
newPos.y = result;
this.transformRef.position = newPos;
break;
case ETransfrom.Rotation:
var newRot = this.transformRef.localEulerAngles;
newRot.y = result;
this.transformRef.localEulerAngles = newRot;
break;
case ETransfrom.Scale:
var newScale = this.transformRef.localScale;
newScale.y = result;
this.transformRef.localScale = newScale;
break;
}
});
}
: (ValueChanged<string>) null
)
)),
new Flexible(
child: new Container()
),
new Flexible(
flex: 8,
child: new Container(
decoration: new BoxDecoration(
color: new Color(0xfff5f5f5)),
child: new TextField(
decoration: new InputDecoration(
border: new UnderlineInputBorder(),
contentPadding:
EdgeInsets.symmetric(
horizontal: 10f, vertical: 5f),
labelText: "Z"
),
controller: zValueController,
onChanged: hasRef
? (str) => {
this.setState(() => {
float result = 0;
float.TryParse(str, out result);
this.oldCursorPosition = zValueController.selection.startPos.offset;
Debug.Log(result);
switch (type) {
case ETransfrom.Position:
var newPos = this.transformRef.position;
newPos.z = result;
this.transformRef.position = newPos;
break;
case ETransfrom.Rotation:
var newRot = this.transformRef.localEulerAngles;
newRot.z = result;
this.transformRef.localEulerAngles = newRot;
break;
case ETransfrom.Scale:
var newScale = this.transformRef.localScale;
newScale.z = result;
this.transformRef.localScale = newScale;
break;
}
});
}
: (ValueChanged<string>) null
)
))
}
)
}
);
}
public override Widget build(BuildContext context) {
return new Theme(
data: new ThemeData(
appBarTheme: new AppBarTheme(
color: Colors.purple
),
cardTheme: new CardTheme(
color: Colors.white,
elevation: 2.0f
)
),
child: new Scaffold(
appBar: new AppBar(title: new Text("Custom Inspector")),
body: new ListView(
children: new List<Widget> {
new Card(
clipBehavior: Clip.antiAlias,
margin: EdgeInsets.all(20.0f),
shape: new RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0f)
),
child: new Container(
padding: EdgeInsets.symmetric(vertical: 20f, horizontal: 10f),
child: new Column(
mainAxisSize: MainAxisSize.min,
children: new List<Widget> {
new UnityObjectDetector(
onRelease: (details) => {
this.setState(() => {
this.objectRef = details.objectReferences[0] as GameObject;
if (this.objectRef) {
this.transformRef = this.objectRef.transform;
}
});
},
child: new ListTile(
title: new Text(
this.objectRef == null ? "Object Name" : this.objectRef.name,
style: new TextStyle(fontSize: 28.0f)),
subtitle: new Text("Drag an object here",
style: new TextStyle(fontSize: 16.0f)),
contentPadding: EdgeInsets.symmetric(horizontal: 10f)
)
),
new Card(
clipBehavior: Clip.antiAlias,
shape: new RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0f)
),
child: new Container(
padding: EdgeInsets.symmetric(horizontal: 10.0f),
child: new Column(
mainAxisSize: MainAxisSize.min,
children: new List<Widget> {
new Container(
padding: EdgeInsets.only(top: 20f),
child: new Align(
alignment: Alignment.centerLeft,
child: new Text("Transform",
style: new TextStyle(fontSize: 20.0f))
)
),
this.getCardRow(ETransfrom.Position,
this.objectRef != null),
this.getCardRow(ETransfrom.Rotation,
this.objectRef != null),
this.getCardRow(ETransfrom.Scale, this.objectRef != null),
new Container(padding: EdgeInsets.only(bottom: 20f))
}
)
)
),
}
)
)
)
}
)
)
);
}
}
}

171
Samples/UIWidgetSample/DragNDrop/UnityObjectDetectorSample.cs


using System.Collections.Generic;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.editor;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.widgets;
using UnityEditor;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
namespace UIWidgetsSample.DragNDrop {
public class UnityObjectDetectorSample : UIWidgetsEditorWindow {
[MenuItem("UIWidgetsTests/Drag&Drop/UnityObject Detector")]
public static void ShowEditorWindow() {
var window = GetWindow<UnityObjectDetectorSample>();
window.titleContent.text = "UnityObject Detector Sample";
}
protected override Widget createWidget() {
Debug.Log("[ WIDGET RECREATED ]");
return new WidgetsApp(
home: new UnityObjectDetectorSampleWidget(),
pageRouteBuilder: (RouteSettings settings, WidgetBuilder builder) =>
new PageRouteBuilder(
settings: settings,
pageBuilder: (BuildContext context, Animation<float> animation,
Animation<float> secondaryAnimation) => builder(context)
)
);
}
}
public class UnityObjectDetectorSampleWidget : StatefulWidget {
public UnityObjectDetectorSampleWidget(Key key = null) : base(key) {
}
public override State createState() {
return new UnityObjectDetectorSampleWidgetState();
}
}
public class UnityObjectDetectorSampleWidgetState : State<UnityObjectDetectorSampleWidget> {
readonly Color highlightColor = Color.fromARGB(255, 88, 127, 219);
readonly Color defaultColor = Color.fromARGB(255, 211, 211, 211);
readonly List<bool> isHighlighted = new List<bool> { };
readonly List<Object[]> objects = new List<Object[]>();
List<Widget> getUnityObjectDetectorList(int count) {
if (this.isHighlighted.isEmpty()) {
for (int i = 0; i < count; i++) {
this.isHighlighted.Add(false);
}
}
if (this.objects.isEmpty()) {
for (int i = 0; i < count; i++) {
this.objects.Add(null);
}
}
List<Widget> widgetList = new List<Widget>();
widgetList.Add(this.getGapBox("Generated List with UnityObjectDetector"));
for (int i = 0; i < count; i++) {
var _i = i;
Widget widget = new Container(
decoration: this.isHighlighted[_i]
? new BoxDecoration(color: this.highlightColor)
: new BoxDecoration(color: this.defaultColor),
height: 100f,
child: new UnityObjectDetector(
onEnter: () => {
Debug.Log("Widget " + _i + " onEnter");
this.setState(() => { this.isHighlighted[_i] = true; });
},
onRelease: (details) => {
Debug.Log("Widget " + _i + " onRelease");
this.setState(() => {
this.isHighlighted[_i] = false;
this.objects[_i] = details.objectReferences;
});
},
onExit: () => {
Debug.Log("Widget " + _i + " onExit");
this.setState(() => { this.isHighlighted[_i] = false; });
},
child: new Center(
child: new Text(this.objects[_i] != null
? this.getNameString(this.objects[_i])
: "[Drop/Multi-Drop Here]")
)
)
);
widgetList.Add(widget);
if (_i != count - 1) {
widgetList.Add(this.getGapBox());
}
}
return widgetList;
}
string getNameString(Object[] objs) {
var str = "";
for (int i = 0; i < objs.Length; i++) {
str += "[" + objs[i].name + "]";
if (i != objs.Length - 1) {
str += "\n";
}
}
return str;
}
Widget getGapBox(string str = "") {
return new Container(
height: 25,
child: str == ""
? null
: new Center(
child: new Text(str)
)
);
}
bool highlight;
Object[] objRef;
public override Widget build(BuildContext context) {
var columnList = new List<Widget>();
columnList.Add(this.getGapBox());
columnList.AddRange(this.getUnityObjectDetectorList(3));
columnList.AddRange(
new List<Widget> {
this.getGapBox("With Listener"),
new Container(
decoration: this.highlight
? new BoxDecoration(color: this.highlightColor)
: new BoxDecoration(color: this.defaultColor),
height: 100f,
child: new Listener(
onPointerDragFromEditorEnter: (evt) => { this.setState(() => { this.highlight = true; }); },
onPointerDragFromEditorExit: (evt) => { this.setState(() => { this.highlight = false; }); },
// onPointerDragFromEditorHover: (evt) => { },
onPointerDragFromEditorRelease: (evt) => {
this.objRef = evt.objectReferences;
this.setState(() => { this.highlight = false; });
},
child: new Center(
child: new Text(this.objRef != null
? this.getNameString(this.objRef)
: "[Drop/Multi-Drop Here]")
)
)
)
}
);
return new Container(
padding: EdgeInsets.symmetric(horizontal: 25f),
color: Colors.grey,
child: new ListView(
children: columnList
));
}
}
}

11
Samples/UIWidgetSample/DragNDrop/UnityObjectDetectorSample.cs.meta


fileFormatVersion: 2
guid: 2948179a390a944a9a44b7f86cfa3c57
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

285
Samples/UIWidgetSample/DragNDropSample.cs


using System.Collections.Generic;
using Unity.UIWidgets.animation;
using Unity.UIWidgets.editor;
using Unity.UIWidgets.foundation;
using Unity.UIWidgets.material;
using Unity.UIWidgets.painting;
using Unity.UIWidgets.rendering;
using Unity.UIWidgets.widgets;
using UnityEditor;
using UnityEngine;
using Color = Unity.UIWidgets.ui.Color;
using Transform = UnityEngine.Transform;
namespace UIWidgetsSample {
public class DragNDropSample : UIWidgetsEditorWindow {
[MenuItem("UIWidgetsTests/Drag&Drop/DragNDropSample")]
public static void ShowEditorWindow() {
var window = GetWindow<DragNDropSample>();
window.titleContent.text = "DragNDropSample";
}
protected override Widget createWidget() {
Debug.Log("[ WIDGET CREATED ]");
return new WidgetsApp(
home: new DragNDropSampleWidget(),
pageRouteBuilder: (RouteSettings settings, WidgetBuilder builder) =>
new PageRouteBuilder(
settings: settings,
pageBuilder: (BuildContext context, Animation<float> animation,
Animation<float> secondaryAnimation) => builder(context)
)
);
}
}
public class DragNDropSampleWidget : StatefulWidget {
public DragNDropSampleWidget(Key key = null) : base(key) {
}
public override State createState() {
return new DragNDropSampleWidgetState();
}
}
public class DragNDropSampleWidgetState : State<DragNDropSampleWidget> {
readonly Color highlightColor = Color.fromARGB(255, 88, 127, 219);
readonly Color defaultColor = Color.fromARGB(255, 211, 211, 211);
readonly List<bool> isHighlighted = new List<bool> { };
readonly List<Object[]> objects = new List<Object[]>();
List<Widget> getUnityObjectDetectorList(int count) {
if (this.isHighlighted.isEmpty()) {
for (int i = 0; i < count; i++) {
this.isHighlighted.Add(false);
}
}
if (this.objects.isEmpty()) {
for (int i = 0; i < count; i++) {
this.objects.Add(null);
}
}
List<Widget> widgetList = new List<Widget>();
widgetList.Add(this.getGapBox("Use UnityObjectDetector"));
for (int i = 0; i < count; i++) {
var _i = i;
Widget widget = new Container(
decoration: this.isHighlighted[_i]
? new BoxDecoration(color: this.highlightColor)
: new BoxDecoration(color: this.defaultColor),
height: 100f,
child: new UnityObjectDetector(
onEnter: () => {
Debug.Log("Widget " + _i + " onEnter");
this.setState(() => { this.isHighlighted[_i] = true; });
},
onRelease: (details) => {
Debug.Log("Widget " + _i + " onRelease");
this.setState(() => {
this.isHighlighted[_i] = false;
this.objects[_i] = details.objectReferences;
});
},
onExit: () => {
Debug.Log("Widget " + _i + " onExit");
this.setState(() => { this.isHighlighted[_i] = false; });
},
child: new Center(
child: new Text(this.objects[_i] != null
? this.getNameString(this.objects[_i])
: "[Drop/Multi-Drop Here]")
)
)
);
widgetList.Add(widget);
if (_i != count - 1) {
widgetList.Add(this.getGapBox());
}
}
return widgetList;
}
string getNameString(Object[] objs) {
var str = "";
for (int i = 0; i < objs.Length; i++) {
str += "[" + objs[i].name + "]";
if (i != objs.Length - 1) {
str += "\n";
}
}
return str;
}
Widget getGapBox(string str = "") {
return new Container(
height: 25,
child: str == ""
? null
: new Center(
child: new Text(str)
)
);
}
Widget getSizedTableCell(string text) {
return new Container(
height: 20f,
child: new Center(
child: new Text(text)
)
);
}
bool highlight = false;
Object[] objRef;
bool isTransformDisplayHighlighted = false;
Transform transformRef;
public override Widget build(BuildContext context) {
var columnList = new List<Widget>();
columnList.AddRange(this.getUnityObjectDetectorList(2));
columnList.AddRange(
new List<Widget> {
this.getGapBox("Use Listener"),
new Container(
decoration: this.highlight
? new BoxDecoration(color: this.highlightColor)
: new BoxDecoration(color: this.defaultColor),
height: 100f,
child: new Listener(
onPointerDragFromEditorEnter: (evt) => {
this.setState(() => { this.highlight = true; });
},
onPointerDragFromEditorExit: (evt) => {
this.setState(() => { this.highlight = false; });
},
// onPointerDragFromEditorHover: (evt) => { },
onPointerDragFromEditorRelease: (evt) => {
this.objRef = evt.objectReferences;
this.setState(() => { this.highlight = false; });
},
child: new Center(
child: new Text(this.objRef != null
? this.getNameString(this.objRef)
: "[Drop/Multi-Drop Here]")
)
)
)
}
);
columnList.AddRange(
new List<Widget>() {
this.getGapBox("Sample: Display Transform"),
new Container(
decoration: this.isTransformDisplayHighlighted
? new BoxDecoration(color: this.highlightColor)
: new BoxDecoration(color: this.defaultColor),
height: 100f,
child: new UnityObjectDetector(
onEnter: () => { this.setState(() => { this.isTransformDisplayHighlighted = true; }); },
onRelease: (details) => {
this.setState(() => {
this.isTransformDisplayHighlighted = false;
var gameObj = details.objectReferences[0] as GameObject;
if (gameObj) {
this.transformRef = gameObj.GetComponent<Transform>();
}
else {
Debug.Log("Not a GameObject");
}
});
},
onExit: () => {
Debug.Log("onExit");
this.setState(() => { this.isTransformDisplayHighlighted = false; });
},
child: new Column(
children: new List<Widget> {
new Container(
height: 20,
child: new Center(
child: new Text(this.transformRef == null
? "[Drop a GameObject from scene here]"
: this.transformRef.name
)
)
),
new Table(
border: TableBorder.all(),
children: new List<TableRow> {
new TableRow(
children: new List<Widget> {
this.getSizedTableCell(""),
this.getSizedTableCell("X"),
this.getSizedTableCell("Y"),
this.getSizedTableCell("Z")
}
),
new TableRow(
children: new List<Widget> {
this.getSizedTableCell("Position"),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.position.x.ToString()),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.position.y.ToString()),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.position.z.ToString())
}
),
new TableRow(
children: new List<Widget> {
this.getSizedTableCell("Rotation"),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.eulerAngles.x.ToString()),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.eulerAngles.y.ToString()),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.eulerAngles.z.ToString())
}
),
new TableRow(
children: new List<Widget> {
this.getSizedTableCell("Scale"),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.localScale.x.ToString()),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.localScale.y.ToString()),
this.getSizedTableCell(this.transformRef == null
? ""
: this.transformRef.localScale.z.ToString())
}
)
}
)
}
)
))
}
);
return new Container(
padding: EdgeInsets.symmetric(horizontal: 25f),
color: Colors.grey,
child: new ListView(
children: columnList
));
}
}
}

/Samples/UIWidgetSample/DragNDropSample.cs.meta → /Samples/UIWidgetSample/DragNDrop/CustomInspectorSample.cs.meta

正在加载...
取消
保存