前言
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 Size(formWidth, formHeight);
SizeGripStyle = SizeGripStyle.Show;
labell = new Label();
labell.Text = "窗体1传过来的值:";
labell.Location = new Point(50, 50);
labell.Size = new Size(200, 50);
labell.Font = new System.Drawing.Font("Arial", 20f);
textboxAutoSize = new TextBox();
textboxAutoSize.Text = "";
textboxAutoSize.Location = new Point(50, 150);
textboxAutoSize.Size = new Size(250, 50);
textboxAutoSize.Font = new System.Drawing.Font("Arial", 20f);
Controls.Add(textboxAutoSize);
Controls.Add(labell);
}
}
}
二、点击按钮传值
1.方法1:变量赋值
给另一个窗体文件的变量传值,其代码分别如下。
窗体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);
mybutton.Click += (sender, args) =>
{
var form3 = new OtherForm();
form3.value = textboxAutoSize.Text;
form3.Show();
};
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 string value;
public OtherForm()
{
BackColor = System.Drawing.Color.FromArgb(239, 235, 233);
MinimumSize = new Size(320, 240);
Text = "窗体2";
Size = new Size(formWidth, formHeight);
SizeGripStyle = SizeGripStyle.Show;
labell = new Label();
labell.Text = "窗体1传过来的值:";
labell.Location = new Point(50, 50);
labell.Size = new Size(200, 50);
labell.Font = new System.Drawing.Font("Arial", 20f);
textboxAutoSize = new TextBox();
textboxAutoSize.Text = value;
textboxAutoSize.Location = new Point(50, 150);
textboxAutoSize.Size = new Size(250, 50);
textboxAutoSize.Font = new System.Drawing.Font("Arial", 20f);
Controls.Add(textboxAutoSize);
Controls.Add(labell);
}
}
}
如上图,通过实际验证,这种方式行不通,窗体1在创建窗体2时,给船体2的变量 value 赋值,但是窗体2窗体成功创建后,并不能显示窗体1中的值。如下所示:
2.方法2:控件赋值
给另一个窗体文件的控件赋值,其代码分别如下。
窗体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);
mybutton.Click += (sender, args) =>
{
var form3 = new OtherForm();
form3.textboxAutoSize.Text = textboxAutoSize.Text;
form3.Show();
};
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 Size(formWidth, formHeight);
SizeGripStyle = SizeGripStyle.Show;
labell = new Label();
labell.Text = "窗体1传过来的值:";
labell.Location = new Point(50, 50);
labell.Size = new Size(200, 50);
labell.Font = new System.Drawing.Font("Arial", 20f);
textboxAutoSize = new TextBox();
textboxAutoSize.Text = "";
textboxAutoSize.Location = new Point(50, 150);
textboxAutoSize.Size = new Size(250, 50);
textboxAutoSize.Font = new System.Drawing.Font("Arial", 20f);
Controls.Add(textboxAutoSize);
Controls.Add(labell);
}
}
}
如上图,通过验证,这个方式是能够成功传值的,所以在新建一个窗体时,需要在控件上显示相应 的值可以直接赋值给控件即可。但是对于一些控件,比如说TableView,在创建一个新窗体并显示值时就显得尤为麻烦。这时就需要想想别的办法了。看下面的详解…
2.方法3:中间文件传值
不多说,直接上步骤:
- 新建一个基于MonoBehaviour的C#文件,添加一个变量,用于传递变量的值,代码如下,将该文件添加到MainCamera上。
using UnityEngine;
public class PremeterFile : MonoBehaviour
{
public static string getstr;
public string Mystr { get; }
public PremeterFile(string newstr)
{
this.Mystr = newstr;
getstr = this.Mystr;
}
}
- 改窗体一中按钮点击函数的的程序如下:
mybutton.Click += (sender, args) =>
{
PremeterFile myfile = new PremeterFile(textboxAutoSize.Text);
var form3 = new OtherForm();
form3.Show();
};
- 改窗体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 Size(formWidth, formHeight);
SizeGripStyle = SizeGripStyle.Show;
labell = new Label();
labell.Text = "窗体1传过来的值:";
labell.Location = new Point(50, 50);
labell.Size = new Size(200, 50);
labell.Font = new System.Drawing.Font("Arial", 20f);
textboxAutoSize = new TextBox();
textboxAutoSize.Text =PremeterFile.getstr;
textboxAutoSize.Location = new Point(50, 150);
textboxAutoSize.Size = new Size(250, 50);
textboxAutoSize.Font = new System.Drawing.Font("Arial", 20f);
Controls.Add(textboxAutoSize);
Controls.Add(labell);
}
}
}
- 看结果,这是能够成功传值了,因此对于复杂的矩阵之类的,我觉得就可以这么传值了。仅供参考,有更好的方法情分享一下,,,
总结
方法感觉有点lou,但是能实现我的gui传值,暂且保留记录,以供以后参考,毕竟对于一个外行,只是用这个软件做做简单可视化的我来说,也不想追求有多高级的代码了,能实现就行。