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 ; 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" ) ; } ;
transform. GetChild ( 0 ) . GetComponent < Button> ( ) . onClick. AddListener ( delegate ( ) { Debug. Log ( " OnClick" ) ; } ) ; 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) ; 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) ; 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) ; } PrizeImage. GetComponent < Image> ( ) . overrideSprite = Resources. Load ( "logo" , typeof ( Sprite ) ) as Sprite ; GameObject obj= Resources. Load < GameObject> ( "Item" ) as GameObject ; obj = Instantiate ( obj, Vector3. one, Quaternion. identity) ; obj. transform. SetParent ( transform) ;
Random. Range ( 1 , 3 ) ; 这个结果就是一个随机一个1 , 2 ;不会有3 FileInfo[ ] files = direc. GetFiles ( "*" , SearchOption. AllDirectories) ; if ( files[ j] . Name. EndsWith ( ImageType[ i] ) ) { filePaths. Add ( imagePath + files[ j] . Name) ; } GetComponent < RectTransform> ( ) . sizeDelta = new Vector2 ( width, height) ;
GetComponent < RectTransform> ( ) . anchoredPosition = new Vector2 ( - 303f , 46f ) ;
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) ; }
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 ;
[ 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 )
{
}