private void OnGUI()
{
if (GUILayout.Button("test"))
{
}
}
获取下级物体GameObject
spawnList = new GameObject[transform.childCount]; // 创建一个敌人生成器 链表
for (int i = 0; i < spawnList.Length; i++)
{
spawnList[i] = transform.GetChild(i).gameObject;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、加载预制件
GameObject obj= Resources.Load<GameObject>("Item") as GameObject;
obj.transform.position += transform.right * 0.1f;
obj = Instantiate(obj);
obj.GetComponent<Transform>().SetParent(transform);
// 枚举 获取所有枚举类型
public enum ItemMoveType
{
None,
sphere,//球状
helix,//螺旋状
word,//根据图片中的文字或字母
}
Array array = Enum.GetValues(typeof(ItemMoveType));
None== (ItemMoveType)0;
sphere==(ItemMoveType)1;
/ array存放所有的 枚举类型
/ //Random.onUnitSphere生成一个随机单位球上坐标 三维
tempPos = Random.onUnitSphere * resetRadius;
按钮绑定函数
using UnityEngine.UI;
Button btn = this.GetComponent<Button> ();
UIEventListener btnListener = btn.gameObject.AddComponent<UIEventListener> ();
btnListener.OnClick += delegate(GameObject gb) {
Debug.Log(gb.name + " OnClick");
};
/按钮绑定函数2
transform.GetChild(0).GetComponent<Button>().onClick.AddListener(delegate(){
Debug.Log(" OnClick");
});
/
/yaml配置文件写入
Serializer serializer = new Serializer();
string yamlString = serializer.Serialize(mData);
print("-____________"+ yamlString);
string fp = Application.dataPath + "\\yaml.text";
if (!File.Exists(fp)) // 判断是否已有相同文件
{
FileStream fs1 = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
fs1.Close();
}
File.WriteAllText(fp, yamlString);
// yaml文件的读取
string fp = Application.dataPath + "\\yaml.text";
string yamlString = File.ReadAllText(fp);
Deserializer mDeserializer = new Deserializer();
Data data2= mDeserializer.Deserialize<Data>(yamlString);
print("----"+data2.name+"-----"+data2.password);
文件操作 写文件
using System.IO;
string fp = Application.dataPath + "\\yaml.text";
if (!File.Exists(fp)) // 判断是否已有相同文件
{
FileStream fs1 = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
fs1.Close();
}
File.WriteAllText(fp, yamlString);
//读文件
string fp = Application.dataPath + "\\yaml.text";
string yamlString = File.ReadAllText(fp);
///ugui的Image动态加载assetSteam文件夹下面的jpg图片
public byte[] getImageByte(string imagePath)
{
FileStream files = new FileStream(imagePath, FileMode.Open);
byte[] imgByte = new byte[files.Length];
files.Read(imgByte, 0, imgByte.Length);
files.Close();
return imgByte;
}
public Image image;
if (GUILayout.Button("load assetstream"))
{
Texture2D tx = new Texture2D(100, 100);
tx.LoadImage(getImageByte(Application.dataPath + "\\StreamingAssets\\images\\aratar_276.jpg"));
image.sprite = Sprite.Create(tx, new Rect(0, 0, tx.width, tx.height), Vector2.zero);
}
/ugui的image 直接加载Resources文件夹下面的sprite2d图片,不能加后缀
PrizeImage.GetComponent<Image>().overrideSprite = Resources.Load("logo", typeof(Sprite)) as Sprite;
/从resources文件夹加载预制件
GameObject obj= Resources.Load<GameObject>("Item") as GameObject;
obj = Instantiate(obj, Vector3.one, Quaternion.identity); //实例化之后才能修改属性,第四个参数可以直接设置父物体 obj = Instantiate(obj);这样实例化加载到默认位置
obj.transform.SetParent(transform);
//设置父物体之后才能修改scale等属性
//
Random.Range(1,3);这个结果就是一个随机一个1,2;不会有3
//C#判断文件类型
FileInfo[] files = direc.GetFiles("*", SearchOption.AllDirectories);
if (files[j].Name.EndsWith(ImageType[i]))
{
filePaths.Add(imagePath + files[j].Name); //存放文件夹中所有文件的路径+名字
}
///files[j].Name获取文件名字,不含路径,但是包含后缀。files[j].FullName表示绝对路径
/改变RectTransform的width,height
GetComponent<RectTransform>().sizeDelta = new Vector2(width, height);
//ugui修改位置
GetComponent<RectTransform>().anchoredPosition = new Vector2(-303f, 46f );
/C#存储json文件
public static void store( object obj)
{
string fp = Application.dataPath + "\\ModelList.json";
if (!File.Exists(fp)) // 判断是否已有相同文件
{
FileStream fs1 = new FileStream(fp, FileMode.Create, FileAccess.ReadWrite);
fs1.Close();
}
mModelInfoJson = JsonConvert.SerializeObject(obj);
//File.WriteAllText(fp, mModelInfoJson);
/* string temp = File.ReadAllText(fp);
temp += mModelInfoJson;*/
File.WriteAllText(fp, mModelInfoJson);
}
//读取json文件
public static T readModelList<T>(T t_object )
{
string fp = Application.dataPath + "\\ModelList.json";
T temp = JsonConvert.DeserializeObject<T>(File.ReadAllText(fp)); // 尖括号<>中填入对象的类名
if (temp != null)
{
return temp;
}
else
{
return t_object;
}
}
/
Unity中使用系统API
[System.Runtime.InteropServices.DllImport("kernel32", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、网格合并
using UnityEngine;
using System.Collections;
// Copy meshes from children into the parent's Mesh.
// CombineInstance stores the list of meshes. These are combined
// and assigned to the attached Mesh.
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class ExampleClass : MonoBehaviour
{
void Start()
{
MeshFilter[] meshFilters = GetComponentsInChildren<MeshFilter>();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
int i = 0;
while (i < meshFilters.Length)
{
combine[i].mesh = meshFilters[i].sharedMesh;
combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
meshFilters[i].gameObject.SetActive(false);
i++;
}
transform.GetComponent<MeshFilter>().mesh = new Mesh();
transform.GetComponent<MeshFilter>().mesh.CombineMeshes(combine);
transform.gameObject.SetActive(true);
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
该参数是一个函数,函数的定义在调用的时候传入。
T为泛型,允许任何类型,GameObject为函数返回类型
void function1(Func<T, GameObject> getGameObject )
{
}
Unity的UI界面——Text/Image
2024-07-14 06:26:04 55 阅读