Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unity Memory

Unity Memory

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

Wei Wang

July 17, 2014
Tweet

More Decks by Wei Wang

Other Decks in Programming

Transcript

  1. 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Ҏ֎) ͷ૊Έ߹ΘͤͰ͢
  2. 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 -> ը૾ͱ͔
  3. 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 ཞ༻͠ͳ͍Ͱ
  4. 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·ͩϦϦʔε͞Ε͍ͬͯͳ͍
  5. 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Ͱ΋େৎ෉
  6. 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; }
  7. 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΋