SceneDataSaveComponent.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using WS;
  4. [ObjectSystem]
  5. public class SceneDataSaveComponentAwakSystem : AwakeSystem<SceneDataSaveComponent>
  6. {
  7. public override void Awake(SceneDataSaveComponent self)
  8. {
  9. self.Awake();
  10. }
  11. }
  12. public class SceneDataSaveComponent : SingleComponent<SceneDataSaveComponent>
  13. {
  14. private Dictionary<string, List<SceneData>> data = new Dictionary<string, List<SceneData>>();
  15. public class SceneData
  16. {
  17. public string objName;
  18. public string animName;
  19. }
  20. public void Awake()
  21. {
  22. }
  23. /// <summary>添加</summary>
  24. public void AddSceneData(string sceneName, string objName, string animName)
  25. {
  26. if (data.ContainsKey(sceneName))
  27. {
  28. if (data[sceneName].Find(x=>x.objName== objName)==null)
  29. {
  30. data[sceneName].Add(new SceneData() { objName = objName, animName = animName });
  31. }
  32. else
  33. {
  34. int index = data[sceneName].FindIndex(x => x.objName == objName);
  35. data[sceneName][index].animName = animName;
  36. }
  37. }
  38. else
  39. {
  40. data.Add(sceneName, new List<SceneData>());
  41. data[sceneName].Add(new SceneData() { objName = objName, animName = animName });
  42. }
  43. }
  44. /// <summary>删除流程</summary>
  45. public void RemoveSceneData(string sceneName, string objName, string animName)
  46. {
  47. if (data.ContainsKey(sceneName))
  48. {
  49. var d = data[sceneName].FindAll(x => x.objName == objName);
  50. if (d != null && d.Count != 0 && d.Find(x => x.animName == animName) != null)
  51. {
  52. data[sceneName].Remove(d.Find(x => x.animName == animName));
  53. }
  54. }
  55. }
  56. ///<summary>加载场景数据</summary>
  57. public void LoadScene(string sceneName)
  58. {
  59. if (data.ContainsKey(sceneName))
  60. {
  61. var d = data[sceneName];
  62. for (int i = 0; i < d.Count; i++)
  63. {
  64. var InteractObj = GameObject.Find(d[i].objName);
  65. if (InteractObj != null)
  66. {
  67. var anim = InteractObj.transform.GetComponent<Animator>();
  68. var infos = anim.runtimeAnimatorController.animationClips;
  69. for (int j = 0;j<infos.Length;j++)
  70. {
  71. var info = infos[j];
  72. if(info.name== d[i].animName)
  73. {
  74. anim.Play(d[i].animName, -1, info.length);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. ///<summary>清除单个场景数据</summary>
  82. public void RestScene(string sceneName)
  83. {
  84. if (data.ContainsKey(sceneName))
  85. {
  86. data[sceneName].Clear();
  87. }
  88. }
  89. ///<summary>清除所有数据</summary>
  90. public void RestAllScene()
  91. {
  92. data.Clear();
  93. }
  94. }