Unity3d 初探

你的財神爺發表於2019-01-07

1.Mathf.Abs(Screen.height * 1.0f / Screen.width - 3.0f / 4)-------Screen類下

2.string filePath = Application.persistentDataPath + "/picture.jpg";-------Application類下

3. WaiteForWWW("https://wx.qimengkeji.com.cn/ppg_server/xingwawa/notice.txt", (www) =>  ------WWW類
        {
            string s = www.text;
            if (!showNoticed && !s.Equals(""))
            {
                //if (PlayerPrefs.GetString("notice") != s)
                {
                    showNoticed = true;
                    PlayerPrefs.SetString("notice", s);
                    noticeUi.GetChild(0).GetChild(0).GetChild(0).GetChild(0).GetComponent<Text>().text = s;
                    noticeUi.gameObject.SetActive(true);
                }
            }
        });

4. if (File.Exists(filePath))
        {
            byte[] bytes = File.ReadAllBytes(filePath);
            Texture2D texture = new Texture2D(128, 128);
            texture.LoadImage(bytes);
            Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            panel.transform.GetChild(1).GetChild(2).GetComponent<Image>().sprite = sprite;
        }

5.  Debug.Log(www.text) ------log輸出

6.  PlayerPrefs.SetString("playerId", www.text);-------PlayerPrefs類,本地持久化

7. JsonData jsonData = JsonMapper.ToObject(WWW.UnEscapeURL(www.text));

8.  this.LoadTexture(WxAuthorize.IconUrl(WWW.UnEscapeURL(s[1])), (texture2D) => {
                    Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0, 0));
                    panel.transform.GetChild(1).GetChild(2).GetComponent<Image>().sprite = sprite;
                    string filePath = Application.persistentDataPath + "/picture.jpg";
                    //filePath = Application.dataPath + "/picture.jpg";
                    byte[] bytes = texture2D.EncodeToPNG();
                    File.WriteAllBytes(filePath, bytes);
                });

9.  AndroidJavaObject _ajc= new AndroidJavaObject("com.jing.unity.Unity2Android");  --------jni呼叫
        bool success;
        success = _ajc.Call<bool>("joinQQGroup","yu16-R4oVeHl8CBuwAiaBklC-qgDunel");
        //  success = _ajc.Call<bool>("openApp", "mqqopensdkapi://bizAgent/qm/qr?url=http://qm.qq.com/cgi-bin/qm/qr?from=app&p=android&k=yu16-R4oVeHl8CBuwAiaBklC-qgDunel");
        Debug.Log("QQGroup");  

10. SceneManager.LoadScene("start");----------------------SceneManager類

11.JsonData jsonData = new JsonData();

        foreach (int i in Player.GetAnimals().Keys)
        {
            if(1==Player.GetAnimals()[i])
            {
                JsonData jsonData2 = new JsonData();
                jsonData2["level"] = 1;
                jsonData[Player.PetNameMap()[i]] = jsonData2;
            }
           
        }

        string strJson = jsonData.ToJson();
        string url = "https://wx.qimengkeji.com.cn/ppg_server/xingwawa/index.php?m=xingwawa&a=set_pet_info&id=" +              PassParameter.playerId + "&pet_info=" + WWW.EscapeURL(strJson);
        m.WaiteForWWW(url, (www) => Debug.Log(www.text));

        Debug.Log(strJson);

12. if(!(Application.internetReachability == NetworkReachability.NotReachable))  -----------NetworkReachability類

13.col = GameObject.Find("groundInstance");
        for (int i = 0; i < transform.childCount; i++)  ---------------------------------Transform類

14. public void LateUpdate() {   -----------------------監聽鍵盤輸入
        base.LateUpdate();    if (Input.GetKey("a"))

15.transform.position和transform.localPosition區別

1. position是根據世界原點為中心
2. localPosition是根據父節點為中心,如果沒有父節點,localpositon和position是沒有區別的

16.virtual protected void OnCollisionEnter(Collision collision)==================碰撞檢測
    {
        Character c = collision.gameObject.GetComponent<Character>();

17.GetComponent<Rigidbody>()

18. GetComponent<Rigidbody>().AddForce(collisionDirection * global.instance.bumperForce * a);

19. string names = PlayerPrefs.GetString("names");=================PlayerPrefs類

20.//獲取到按鈕

btn = GameObject.Find(“Button”).GetComponent<Button> ();

//給按鈕新增監聽事件

btn.onClick .AddListener (BtnClick);

void BtnClick() { //按鈕響應事件

Debug.Log (“btn.onClick .AddListener ()”);

}    

21.

Slider是滑動條,Slider的屬性如圖所示:

Slider slider ;

void Star t () {

//獲取到Slider元件

slider = GameObject.Find(“Slider ”).GetComponent<Slider> ();

//新增監聽事件

slider.onValueChanged.AddListener(SliderValueChange);

}

//  事件響應

public voidSliderValueChange (float value){

Debug.Log (“value = “+value);

}
22.

private void OnApplicationQuit()
    {
        SocketManager.Instance.Close();
    }

    private void OnApplicationFocus(bool focus)
    {
        if(focus)
        {
            print("OnApplicationFocus true");
        }else
        {
            print("OnApplicationFocus false");
        }
    }

    private void OnApplicationPause(bool pause)
    {
        if (pause)
        {
            print("OnApplicationPause true");
        }
        else
        {
            print("OnApplicationPause false");
        }
    }

23.