(二)暗黑之光RPG游戏实战心得

来源:互联网 发布:江西银行网络金融部 编辑:程序博客网 时间:2024/04/27 22:06

第二个场景

居然在做完整个项目之后还有兴趣更新博客,这无疑给与了我更大的收获。

好了,废话不多说。

第二个场景是角色选择场景。

技术的重点在于选择按钮切换人物,和保留例如:角色名之类的,用到PlayerPrefs。


实例化角色

首先在Character上申明GameObject的数组,来获取整个角色人物的长度。

length = CharacterPrefab.Length;



new 一个gameObject数组作为实例化角色的容器

在C#中,支持动态数组。
CharacterGameObject = new GameObject[length];
</pre><pre name="code" class="csharp">然后遍历循环
<pre name="code" class="csharp">        for (int i = 0; i < length; i++)        {            CharacterGameObject[i] = GameObject.Instantiate(CharacterPrefab[i], transform.position, transform.rotation) as GameObject;        }

完成实例化。

切换角色

切换角色,自然需要切换按钮。这里只贴切换按钮的核心代码
    //Next按键的回调函数    public void OnNextButtonClick()    {        selectedIndex++;        //无限循环        selectedIndex %= length;        UpdateCharaterShow();    }
    //Prev按键的回调函数    public void OnPrevButtonClick()    {        selectedIndex--;        if (selectedIndex == -1)        {            selectedIndex = length - 1;        }        UpdateCharaterShow();    }


保存用户名

鉴于方便,这里直接没用数据流去存储。

用Unity3D自带的 playerPrefs

支持string , float , int 格式

        //存储角色        PlayerPrefs.SetInt("SelectedCharacterIndex", selectedIndex);        //角色名字        PlayerPrefs.SetString("name",nameInput.value);







0 0
原创粉丝点击