C# 如何通过脚本在Unity3D中动态生成Gui?
我在尝试在Unity3D中动态生成Gui时遇到了一些问题。 我小时候做过一张有面板的画布。我正在尝试用7x4图像填充此面板 我收到一个json对象,它有x个项目。根据项目数量,我用收到的项目数量填充面板。例如,如果我收到一个包含28项(7x4)的Json对象,我希望GUI如下所示:C# 如何通过脚本在Unity3D中动态生成Gui?,c#,user-interface,unity3d,C#,User Interface,Unity3d,我在尝试在Unity3D中动态生成Gui时遇到了一些问题。 我小时候做过一张有面板的画布。我正在尝试用7x4图像填充此面板 我收到一个json对象,它有x个项目。根据项目数量,我用收到的项目数量填充面板。例如,如果我收到一个包含28项(7x4)的Json对象,我希望GUI如下所示: public GameObject canvas; //I use this to set the canvas after applying this script to an empty gameobject p
public GameObject canvas; //I use this to set the canvas after applying this script to an empty gameobject
public GameObject panel; // I do the same for the panel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GUIBuilder : MonoBehaviour
{
public GameObject canvas; //I use this to set the canvas after applying this script to an empty gameobject
public GameObject panel; // I do the same for the panel
public Vector2 ImageViewCount; // 6 * 4
public Vector2 ImageViewSize; // 80 * 80
public Vector2 InitialImageViewPosition; // -300 * 250
public Vector2 ImageViewPositionOffset; // 125 * 200
// Use this for initialization
void Start()
{
GenerateImageView();
}
void GenerateImageView()
{
for (int a = 0; a < ImageViewCount.y; a++)
{
for (int b = 0; b < ImageViewCount.x; b++)
{
ImageViewBuilder(ImageViewSize,
new Vector2(InitialImageViewPosition.x + (ImageViewPositionOffset.x * b),
InitialImageViewPosition.y - (ImageViewPositionOffset.x * a)),
panel.transform);
}
}
}
void ImageViewBuilder(Vector2 size, Vector2 position, Transform objectToSetImageView)
{
GameObject imageView = new GameObject("ImageView", typeof(RectTransform));
RawImage image = imageView.AddComponent<RawImage>();
//image.texture = Your Image Here
RectTransform rectTransform = imageView.GetComponent<RectTransform>();
rectTransform.sizeDelta = size;
rectTransform.anchoredPosition = position;
imageView.transform.SetParent(objectToSetImageView, false);
}
}
如我所说,我在编辑器中制作了一个带有面板的画布。在脚本中,我做了以下几点:
public GameObject canvas; //I use this to set the canvas after applying this script to an empty gameobject
public GameObject panel; // I do the same for the panel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GUIBuilder : MonoBehaviour
{
public GameObject canvas; //I use this to set the canvas after applying this script to an empty gameobject
public GameObject panel; // I do the same for the panel
public Vector2 ImageViewCount; // 6 * 4
public Vector2 ImageViewSize; // 80 * 80
public Vector2 InitialImageViewPosition; // -300 * 250
public Vector2 ImageViewPositionOffset; // 125 * 200
// Use this for initialization
void Start()
{
GenerateImageView();
}
void GenerateImageView()
{
for (int a = 0; a < ImageViewCount.y; a++)
{
for (int b = 0; b < ImageViewCount.x; b++)
{
ImageViewBuilder(ImageViewSize,
new Vector2(InitialImageViewPosition.x + (ImageViewPositionOffset.x * b),
InitialImageViewPosition.y - (ImageViewPositionOffset.x * a)),
panel.transform);
}
}
}
void ImageViewBuilder(Vector2 size, Vector2 position, Transform objectToSetImageView)
{
GameObject imageView = new GameObject("ImageView", typeof(RectTransform));
RawImage image = imageView.AddComponent<RawImage>();
//image.texture = Your Image Here
RectTransform rectTransform = imageView.GetComponent<RectTransform>();
rectTransform.sizeDelta = size;
rectTransform.anchoredPosition = position;
imageView.transform.SetParent(objectToSetImageView, false);
}
}
在Start方法中,我将画布设置为面板的父级:
panel.transform.SetParent(canvas.transform, false);
现在我将面板设置为画布的子级,我希望生成7x4图像。有人能帮我吗?生成多个UI/图像并使其如图所示显示在面板中的最佳方法是什么
Unity3D非常新,我希望更熟悉脚本编写。我认为最好的方法是在面板上使用布局网格组,然后将图像设置为面板的子级,并强制重建布局
为什么要从代码中将画布设置为面板的父级 因此,如果需要从代码动态生成所有图像,可以执行以下操作:
public GameObject canvas; //I use this to set the canvas after applying this script to an empty gameobject
public GameObject panel; // I do the same for the panel
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GUIBuilder : MonoBehaviour
{
public GameObject canvas; //I use this to set the canvas after applying this script to an empty gameobject
public GameObject panel; // I do the same for the panel
public Vector2 ImageViewCount; // 6 * 4
public Vector2 ImageViewSize; // 80 * 80
public Vector2 InitialImageViewPosition; // -300 * 250
public Vector2 ImageViewPositionOffset; // 125 * 200
// Use this for initialization
void Start()
{
GenerateImageView();
}
void GenerateImageView()
{
for (int a = 0; a < ImageViewCount.y; a++)
{
for (int b = 0; b < ImageViewCount.x; b++)
{
ImageViewBuilder(ImageViewSize,
new Vector2(InitialImageViewPosition.x + (ImageViewPositionOffset.x * b),
InitialImageViewPosition.y - (ImageViewPositionOffset.x * a)),
panel.transform);
}
}
}
void ImageViewBuilder(Vector2 size, Vector2 position, Transform objectToSetImageView)
{
GameObject imageView = new GameObject("ImageView", typeof(RectTransform));
RawImage image = imageView.AddComponent<RawImage>();
//image.texture = Your Image Here
RectTransform rectTransform = imageView.GetComponent<RectTransform>();
rectTransform.sizeDelta = size;
rectTransform.anchoredPosition = position;
imageView.transform.SetParent(objectToSetImageView, false);
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类GUIBuilder:单行为
{
public GameObject canvas;//将此脚本应用于空游戏对象后,我使用它设置画布
public GameObject panel;//我对该面板执行相同的操作
公共向量2 ImageViewCount;//6*4
公共向量2 ImageViewSize;//80*80
公共向量2 InitialImageViewPosition;//-300*250
公共向量2 ImageViewPositionOffset;//125*200
//用于初始化
void Start()
{
GenerateImageView();
}
void GenerateImageView()
{
对于(int a=0;a
设法修复了我的代码。
我是如何做到的:
1.在编辑器中添加一个空的游戏对象
2.将带有面板的画布添加为子级
3.将以下脚本添加到步骤1中的空游戏对象:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class generateUI : MonoBehaviour
{
public GameObject canvas;
public GameObject Panel;
public GameObject image;
int size = 40;
private float scaler = 0.0125f;
void Start()
{
Panel.transform.SetParent(canvas.transform, false);
GameObject[] tiles = new GameObject[size];
Vector3 change = new Vector3(20 * scaler, 0, 0);
for (int i = 0; i < size; i++)
{
tiles[i] = GameObject.Instantiate(image, transform.position, transform.rotation);
tiles[i].transform.position += change;
tiles[i].transform.SetParent(Panel.transform, false);
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类生成:单行为
{
公共游戏对象画布;
公共游戏对象面板;
公众游戏对象形象;
int size=40;
专用浮动定标器=0.0125f;
void Start()
{
Panel.transform.SetParent(canvas.transform,false);
GameObject[]tiles=新游戏对象[大小];
Vector3更改=新Vector3(20*定标器,0,0);
对于(int i=0;i
4:现在将以下脚本添加到画布对象:
using System.Collections
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI
public class DynamicGrid : MonoBehaviour
{
public int col,row;
void Start()
{
RectTransform parent = gameObject.GetComponent<RectTransform>();
GridLayoutGroup grid = gameObject.GetComponent<GridLayoutGroup>();
grid.cellSize = new Vector2(parent.rect.width / col, parent.rect.height / row);
}
void Update()
{
}
}
使用System.Collections
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI
公共类动态网格:单行为
{
公共int col,世界其他地区;
void Start()
{
RectTransform parent=gameObject.GetComponent();
GridLayoutGroup grid=gameObject.GetComponent();
grid.cellSize=新矢量2(parent.rect.width/col,parent.rect.height/row);
}
无效更新()
{
}
}
5:将画布宽度设置为1090,高度设置为430
6:从图像(100乘100)制作预制件
7:将栅格布局组添加到配电盘
8:将CellSize设置为100乘100,将间距设置为10乘10
9:将generateUI脚本中的大小更改为您希望看到的数字
10:利润
我现在有了想要的功能。我按照@jour的建议使用gridlayout,我以前在使用python的Kivy软件创建移动应用程序时使用过gridlayout