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,heightGetComponent<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脚步.txt
2025/9/18 20:47:01
来源:https://blog.csdn.net/weixin_49367526/article/details/140403719
浏览:
次
关键词:Unity脚步.txt
版权声明:
本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。
我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com