UIWidgets基础

UIWidgets基础

UIWidgets基础

如果您完成了入门指南并快速创建了您的UIWidgets App应用程序,继续以下步骤,将有助于您进一步深入了解UIWidgets的强大之处!

如何创建UIWidgets面板?

UIWidgets应用程序具有一些基本元素:

需要继承 UIWidgetsPanel;

包含 OnEnable() 函数,可以执行一些初始化操作,如果不需要可以省略;

必须包含 main() 函数,它是整个UIWidgets App程序的入口。

以下示例代码以UIWidgetsPanelDemo为例,展示基本代码结构:

using Unity.UIWidgets.engine;

using Unity.UIWidgets.widgets;

using ui_ = Unity.UIWidgets.widgets.ui;

namespace UIWidgetsSample

{

public class UIWidgetsPanelDemo : UIWidgetsPanel

{

protected new void OnEnable()

{

base.OnEnable();

// 您可以在此处执行一些初始化操作

//如果不需要,可以将其省略掉

}

protected override void main()

{

// 这是您的UIWidgets App程序的入口

// 您可以用自己的部件类来替换这里的“ MyApp()”

ui_.runApp(new MyApp());

}

class MyApp : StatelessWidget

{

public override Widget build(BuildContext context)

{

// 在这里定义您自己的部件树, 以下是一个非常简单的例子

// 在面板中显示“ Hello World”

return new Container(child: new Text("Hello World"));

}

}

}

}

如何创建一个UIWidgets的编辑器窗口?

在UIWidgets编辑器窗口定制化创建编辑器面板:

必须继承于 UIWidgetsEditorPanel;

包含一个名为 OnEnable()的函数,您可以执行一些初始化操作。 如果不需要则可以忽略;

必须包含函数 main(),它是整个UIWidgets App程序的入口。

以下示例代码以EditorWindowSample为例,展示基本代码结构:

using Unity.UIWidgets.Editor;

using Unity.UIWidgets.widgets;

using UnityEditor;

namespace UIWidgetsEditorWindowSample

{

public class EditorWindowSample : UIWidgetsEditorPanel

{

[MenuItem("UIWidgets/EditorSample")]

public static void EditorWindowDemo()

{

CreateWindow();

}

protected override void onEnable()

{

// 可以在此处执行一些初始化操作,比如:

// AddFont("Material Icons", new List {"MaterialIcons-Regular.ttf"}, new List {0});

// 如果不需要请忽略

}

protected override void main()

{

// 这是您的UIWidgets App程序的入口

// 您可以在“ MyApp”类中定义小部件,也可以将其替换为自己的应用程序

ui_.runApp(new MyApp());

}

class MyApp : StatelessWidget

{

// 在此类中,您可以定义自己的窗口部件树,它将显示在编辑器窗口中;

// 以下是一个简单的示例,在“编辑器窗口”中显示“ Hello World!”。

public override Widget build(BuildContext context)

{

return new Container(child: new Text("Hello World in Editor Window!"));

}

}

}

}

您可以通过“UIWidgets-> EditorSample”启动该示例编辑器窗口

如何在编辑器/运行时中切换调试/发布模式?

在Unity编辑器中,您可以通过“UIWidgets-> EnableDebug”切换调试/发布模式。

如果要在运行时更改其他模式,请通过将“com.unity.uiwidgets/com.unity.uiwidgets/Runtime/foundation/debug.cs” 的 “static bool debugEnableAtRuntimeInternal”设置修改为true或false 来实现。 请注意,默认情况下该值设置为false。

如何添加自定义字体?

UIWidgets 可以让您使用自定义字体,有两种添加字体的方式:

您可以如以下所示在Unity Editor中添加字体:

您也可以直接在代码中添加字体,如下所示:

protected override void onEnable()

{

AddFont("GalleryIcons", new List {"gallery/GalleryIcons.ttf"}, new List {0});

}

然后,您还可以定义您的自定义数据。 例如,添加字体“GalleryIcons.ttf”后您可以这样定制图标:

public static class GalleryIcons

{

public static readonly IconData tooltip = new IconData(0xe900, fontFamily: "GalleryIcons");

}

最后,您可以使用刚刚定义的图标:

new GalleryDemo(

title: "Tooltips",

subtitle: "Short message displayed on long-press",

icon: GalleryIcons.tooltip,

category: GalleryDemoCategory._kMaterialComponents,

routeName: "/material/tooltips",

documentationUrl: "https://docs.flutter.io/flutter/material/Tooltip-class.html",

buildRoute: (BuildContext context) => new TooltipDemo()

)

如何部署到IOS / Android?

UIWidgets应用程序的打包方式与一般的Unity应用程序相同,您可以通过“File->Build Settings...”,选择特定平台等步骤来打包UIWidgets App应用程序。

在打包之前,请不要忘记设置 Player Settings。 如下图所示,正确设置包名称,版本号以及其他信息。 一切完成后,单击“build” 完成打包。

相关文章