Unity3D中嵌入winform窗体应用程序:成长之路二

文章目录

  • 前言
  • 一、先创建两个自己的窗体1和2
  • 二、点击按钮传值
    • 1.方法1:变量赋值
    • 2.方法2:控件赋值
    • 2.方法3:中间文件传值
  • 总结


前言

Unity3D中嵌入winform窗体应用程序,成长之路一中介绍了如何用示例窗体类文件在unity场景中创建一个窗体应用程序。经过一段时间的示例文件解读,成功的创建了自己的窗体程序,实现了自己想要的一些些结果,在这成长之路里面只记录一些过程中会遇到的问题,并不会逐一的讲解如何在unity场景中开发属于自己的窗体程序。感兴趣的可以私下交流学习。本期成长之路二是为了记录在窗体程序自定义类之间如何实现数据的传递,分为两种方式的传值,其一是变量型传值,即给另一个窗体内的变量传值;其二是控件值传值,即给另一个窗体的控件赋值。两种实现的结果有所差异,说白了就是一个能成功,一个不能成功,不知道为啥。可能资深尚欠,不知道其中的道道,也不知道不知道我这个办法是不是最好的办法,暂时也没有想到其他的方法。暂且先记录着,反正能用。

一、先创建两个自己的窗体1和2

首先新建两个C#文件,删除所有代码,改为两个窗体类文件,代码如下。在窗体上随便创建了几个控件。

窗体1 的程序:

namespace UnityWinForms.Examples
{
   
    using System.Drawing;
    using System.Windows.Forms;
    public sealed class NewForm : Form
    {
   
        public int formWidth = 500;
        public int formHeight = 800;
        public NewForm()
        {
   
            BackColor = System.Drawing.Color.FromArgb(239, 235, 233);
            MinimumSize = new Size(320, 240);
            Text = "窗体1";
            Size = new Size(formWidth, formHeight);
            SizeGripStyle = SizeGripStyle.Show;
            var textboxAutoSize = new TextBox();
            textboxAutoSize.Text = "input here";
            textboxAutoSize.Location = new Point(50 , 50);
            textboxAutoSize.Size = new Size(200,50);
            textboxAutoSize.Font = new System.Drawing.Font("Arial", 20f);
            var labelAutosize = new Label();
            labelAutosize.Location = new Point(260, 50);
            labelAutosize.Size = new Size(200, 50);
            labelAutosize.Font = new System.Drawing.Font("Arial", 20f);
            textboxAutoSize.TextChanged += (sender, args) => labelAutosize.Text = textboxAutoSize.Text;
            labelAutosize.Text = textboxAutoSize.Text;
            labelAutosize.BorderStyle = BorderStyle.FixedSingle;
            var mybutton = new Button();
            mybutton.Text = "传值";
            mybutton.Location = new Point(50,200);
            mybutton.Size = new Size(250,50);
            mybutton.Font = new System.Drawing.Font("Arial", 20f);
            Controls.Add(textboxAutoSize);
            Controls.Add(labelAutosize);
            Controls.Add(mybutton);
        }
    }
}

窗体2的程序:

namespace UnityWinForms.Examples
{
   
    using System.Drawing;
    using System.Windows.Forms;
    public sealed class OtherForm : Form
    {
   
        public int formWidth = 500;
        public int formHeight = 800;
        public Label labell;
        public TextBox textboxAutoSize;
        public OtherForm()
        {
   
            BackColor = System.Drawing.Color.FromArgb(239, 235, 233);
            MinimumSize = new Size(320, 240);
            Text = "窗体2";
            Size = new 

你可能感兴趣的:(我的阶梯,winform,c#,unity3d)