小言_互联网的博客

Unity学习笔记:人物换装换武器

400人阅读  评论(0)

知识点:对数据的本地化存储和取用(PlayerPrefs 类)

诞生角色

  
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// 诞生角色
  5. /// </summary>
  6. public class BirthPlayer : MonoBehaviour
  7. {
  8. private int clothIndex = 0;
  9. public GameObject player;
  10. public Texture[] cloth;
  11. private void Awake()
  12. {
  13. if (PlayerPrefs.HasKey( "Cloth"))
  14. clothIndex = PlayerPrefs.GetInt( "Cloth");
  15. }
  16. private void Start()
  17. {
  18. GameObject clone =
  19. (GameObject)Instantiate(player, transform.position, transform.rotation);
  20. clone.GetComponentInChildren<SkinnedMeshRenderer>().material.mainTexture
  21. = cloth[clothIndex];
  22. }
  23. }
更换服装

  
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. /// <summary>
  5. /// 更换服装
  6. /// </summary>
  7. public class ChangeCloth : MonoBehaviour
  8. {
  9. private SkinnedMeshRenderer clothRender;
  10. public Texture[] cloth;
  11. private int clothIndex = 0;
  12. private void Start()
  13. {
  14. clothRender = this.GetComponentInChildren<SkinnedMeshRenderer>();
  15. }
  16. private void OnGUI()
  17. {
  18. if (GUILayout.Button( "服装01"))
  19. {
  20. clothRender.material.mainTexture = cloth[ 0];
  21. clothIndex = 0;
  22. }
  23. if (GUILayout.Button( "服装02"))
  24. {
  25. clothRender.material.mainTexture = cloth[ 1];
  26. clothIndex = 1;
  27. }
  28. if (GUILayout.Button( "保存并进入游戏"))
  29. {
  30. PlayerPrefs.SetInt( "Cloth", clothIndex);
  31. SceneManager.LoadScene( "Game");
  32. }
  33. }
  34. }

换武器


  
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.SceneManagement;
  4. /// <summary>
  5. /// 更换武器
  6. /// </summary>
  7. public class ChangeCloth : MonoBehaviour
  8. {
  9. private SkinnedMeshRenderer clothRender;
  10. public Texture[] cloth;
  11. private int clothIndex = 0;
  12. public GameObject weapon; //武器
  13. private MeshFilter meshFilter; //武器网格组件
  14. private MeshRenderer weaponRender;
  15. public Mesh wpMesh; //武器网格
  16. public Texture wpTexture; //武器图片
  17. private void Start()
  18. {
  19. meshFilter = weapon.GetComponent<MeshFilter>();
  20. weaponRender = weapon.GetComponent<MeshRenderer>();
  21. clothRender = this.GetComponentInChildren<SkinnedMeshRenderer>();
  22. }
  23. private void OnGUI()
  24. {
  25. if (GUILayout.Button( "服装01")) {
  26. //更换武器
  27. meshFilter.mesh = wpMesh; //换网格
  28. weaponRender.material.mainTexture = wpTexture; //换网格对应图片
  29. clothRender.material.mainTexture = cloth[ 0];
  30. clothIndex = 0;
  31. }
  32. if (GUILayout.Button( "服装02"))
  33. {
  34. clothRender.material.mainTexture = cloth[ 1];
  35. clothIndex = 1;
  36. }
  37. if (GUILayout.Button( "保存并进入游戏"))
  38. {
  39. PlayerPrefs.SetInt( "Cloth", clothIndex);
  40. SceneManager.LoadScene( "Game");
  41. }
  42. }
  43. }

 


转载:https://blog.csdn.net/huanyu0127/article/details/106087034
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场