Unity Memory

Unity Memory

How to optimize memory usage of Unity. Made for Unity team in Kayac.

318643095c83b914cf80a7f99f247fe6?s=128

Wei Wang

July 17, 2014
Tweet

Transcript

  1. Unity ϝϞϦʔ࠷దԽ Ԧቨ 2014-07-17

  2. None
  3. None
  4. None
  5. མͪΔ མͪΔ མͪΔ σʔλͷಡΈࠐΉ ॲཧ͍ͯ͠ͳ͍Τϥʔ ֎෦ϓϥάΠϯͷѱ͍ ϝϞϦʔ଍Γͳ͍ ʁʁʁ େࣄͳ͜ͱ ͩΕͰ΋෼͔Δ͜ͱඞཁʂ

  6. iPhone 4 iPhone 4s iPad mini: 512M iPhone 5Ҏ্: 1G

    Androidେମ: 1Gɺ2G΋͋Δ ىಈ͢ΔͱɺγεςϜ΋ϝϞϦʔ͔͔Γ·͢ iOS 6 -> 70M Ϩϕϧ iOS 7 -> 200M Ϩϕϧ *iOS 7.1.2ʹվળग़དྷ·͕ͨ͠ɺ170M͙Β͍ ݁࿦ɿϝϞϦʔҰ൪ݫ͍͠ͷ͸ iPhone 4 ͱ iOS 7 (7.1.2Ҏ֎) ͷ૊Έ߹ΘͤͰ͢
  7. WHY ϝϞϦʔਃ੥ ϝϞϦʔ࢖͏ ऴΘͬͨ ϦϦʔε GC.Collect

  8. Unity ϝϞϦʔ؅ཧϧʔϧ 1͔ͭ͋͠Γ·ͤΜʂ ࢀর͕͋Ε͹ɺ ϦϦʔεʢճऩʣͰ͖·ͤΜ

  9. ࠷ॳ͔ΒϝϞϦʔਖ਼͘͠࢖༻ͷҙࣝΛ෇͍ͯɺ ϧʔϧΛकΕ͹ɺ࠷దԽͷ࡞ۀ͕؆୯ʹͳΒΕ·͢ʢͳ͠ʁʣ ͜ͱ͔͠Βɻɻɻ

  10. ࢀরʁ ը૾ʢAtlasɼTextureɼSpriteͳͲʣ Ի੠ʢSEɼBGMɼϘΠεͳͲʣ ΦϒδΣΫτʢεΫϦϓτɼModel dataʣ UnityͷΞηοτ C#

  11. ࢀরྫ GameScene->Sprite(GameObject)-> UISprite->StartAltas(UIAtlas) ->StartAtlas(Material)->StartAtlas.png ϝϞϦʔҰ൪

  12. NGྫ 1 ࢖͍ͬͯͳ͍Sceneͱ͔ Destroyͯ͠ͳͯ͘ɺ SetActive false͚ͩ ͓͢͢Ίɿ࢖͍ͬͯͳ͍΋ͷdestroyͯ͠ɺඞཁͳ࣌࠶Load ॏͯ͘ɺΑ͘࢖͏΋ͷ͕CacheͰ΋OK

  13. NGྫ 2 Talk.cs private static OnComplete _onTalkComplete = null; public

    static void Open(StoryData story, OnComplete onTalkComplete) { _story = story; _onTalkComplete = onTalkComplete; MakeATalk().Show(); } //Talk animation finished: public void OnTalkFinished() { if (_onTalkComplete != null) { _onTalkComplete(); } Destory(this) } Script.cs public void Go() { Talk.Open(story, ()=> { Debug.Log(“Talk finished”); }); } _onTalkComplete -> Script.cs -> Script GameObject -> ը૾ͱ͔
  14. NGྫ 2 Talk.cs private OnComplete _onTalkComplete = null; public void

    Open(StoryData story, OnComplete onTalkComplete) { _story = story; _onTalkComplete = onTalkComplete; Show(); } //Talk animation finished: public void OnTalkFinished() { if (_onTalkComplete != null) { _onTalkComplete(); } Destory(this) } Script.cs public void Go() { Talk t = new Talk(); t.Open(story, ()=> { Debug.Log(“Talk finished”); }); } ͓͢͢Ί staticඞཁͳ͍΋ͷɺstatic͠ͳ͍Ͱ staticɺ΋͘͠͸ singleton ཞ༻͠ͳ͍Ͱ
  15. NGྫ 3 AssetBundle AssetBundle1 = AssetBundle.CreateFromFile("1.unity3d"); obj1.renderer.material.mainTexture = AssetBundle1.Load("wall") as

    Texture; obj2.renderer.material.mainTexture = obj1.renderer.material.mainTexture; //… Destroy(obj1); Destroy(obj2); Assetͷࢀর͕ͳ͘ͳΔ͚Ͳɺ AssetBundle·ͩϦϦʔε͞Ε͍ͬͯͳ͍
  16. NGྫ 3 AssetBundle AssetBundle1 = AssetBundle.CreateFromFile("1.unity3d"); obj1.renderer.material.mainTexture = AssetBundle1.Load("wall") as

    Texture; obj2.renderer.material.mainTexture = obj1.renderer.material.mainTexture; //… Destroy(obj1); Destroy(obj2); AssetBundle1.Unload(true); //͔͔࣌ؒΓ·͢ Resources.UnloadUnusedAssets(); ը૾ϦϦʔεɺdelayͰ΋େৎ෉
  17. 1 2 3 Profiler Window (Cmd+7) ։ൃͷ࣌΋ͪΐͬͱݟ͓ͯئ͍ https://www.youtube.com/watch?v=sBpXiJ9G3OY https://docs.unity3d.com/351/Documentation/Manual/Profiler.html

  18. NGྫ 4 ϦʔΫͰ͸ͳ͍͚Ͳɺ GC AllocͰύʔϑΥʔϚϯεѱ͘ͳΔ public void Update() { GameController

    controller = GetComponent<GameController>(); controller.Foo(); List<GameCharacter> characters = new List<GameCharacter>(); transform.parent = practiceControler.cardRoot; transform.localScale = initCardScale; transform.position = initCardPosition; }
  19. NGྫ 4 public void Awake() { _cachedController = GetComponent<GameController>(); _characters

    = new List<GameCharacter>(); _cachedTransform = transform; } public void Update() { _cachedController.Foo(); _characters.Clear(); _cachedTransform.parent = practiceControler.cardRoot; _cachedTransform.localScale = initCardScale; _cachedTransform.position = initCardPosition; } GameObject.Find ͳͲͷ୳͢method΋
  20. GC Alloc ຖFrameɹϝϞϦʔਃ੥ͷྔ kbͷϨϕϧʹकΓ·͠ΐ͏ GC.Collect গͳ͘ͳΔɺύʔϑΥʔϚϯεΞοϓ

  21. None