Unity3d基础知识和方法

本文介绍了Unity3d中的一些基本操作和常用API的使用,包括Screen类、Application类、WWW类的使用,涉及文件读写、纹理加载、网络请求、持久化数据存储等方面,还涵盖了碰撞检测、日志输出、场景管理等多个关键知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.挂载到游戏物体的一个脚本xxx.cs

其中:this指当前脚本。transform当前物体的transform组件。gameObject当前脚本所挂的物体

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值