1.这是动态生成sprite,并有部分属性操作代码
2.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
private GameObject gob;//物体对象
private Transform m_transform;//Transform对象
void Start()
{
gob = new GameObject("MySprit");//相当于给实体对象命名
m_transform = gameObject.GetComponent<Transform>();//获取脚本依附物体的Transform组件
//动态添加UISprit组件
UISprite sprite = gob.AddComponent<UISprite>();//给gob添加UISprite组件,并把这个组件赋给sprite对象
sprite.atlas = Resources.Load<UIAtlas>("MyAtlas");//sprite对象的atlas属性加载在Resources文件里的MyAtlas
sprite.spriteName = "11000794";//sprite对象的sprite属性用名为11000794的材质
gob.GetComponent<Transform>().SetParent(m_transform);//设置到脚本所依附的物体里
}
void Update()
{
}
}
3.
本文介绍如何在Unity中通过代码动态创建一个带有UISprite组件的游戏对象,并对其进行属性设置,包括加载特定的UIAtlas和SpriteName,同时将其作为当前脚本所在物体的子物体。

被折叠的 条评论
为什么被折叠?



