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

StudyUnityFromAnti-pattern

Cova8bitdots
November 20, 2019

 StudyUnityFromAnti-pattern

Unity の使い方のアンチパターンを紹介。
そこからどう解消すべきかを紹介しています

Cova8bitdots

November 20, 2019
Tweet

More Decks by Cova8bitdots

Other Decks in Programming

Transcript

  1. Ξϯνύλʔϯ͔ΒֶͿ UNITYͷ࢖͍ํ Gotanda.unity #14

  2. Ξϯνύλʔϯྫ Public ม਺໰୊ Prefab ͷ SerializeField ໰୊ Awake / Start

    ໰୊ ิؒؔ਺ ໰୊ Shader ໰୊
  3. Ξϯνύλʔϯྫ Public ม਺໰୊ SerializeField / Prefab ໰୊ Awake / Start

    ໰୊ ิؒؔ਺ ໰୊ Shader ໰୊
  4. PUBLICม਺໰୊ ໰୊఺ ▸ MonoBehaviour ܧঝΫϥεͰ͸Serializeର৅ʹͳΔ ▸ Unity Ͱ͸ Serialize ର৅ʹͳΔͨΊInspector͔Βฤू͕Մೳ

    ▸ উखʹฤूͤͨ͘͞ͳ͍࣌΋͋Δ
 ▸ ֎෦ʹΑΓ؆୯ʹॻ͖׵͑Մೳ ▸ ϓϩύςΟͰpublic ͳSetter Λఏڙ͍ͯ͠Δ৔߹΋ಉ༷ ▸ ΧϓηϧԽ(৘ใӅṭ)తʹඇৗʹϚζΠ
 جຊతʹ֎෦ΫϥεʹΑΔ௚઀ૢ࡞͸ةݥ
  5. PUBLICม਺໰୊ ղফ๏ ▸ MonoBehaviour ܧঝΫϥεͰ͸Serializeର৅ʹͳΔ ▸ [SerializeField] private / protected

    ʹ͠·͠ΐ͏ ▸ Public ม਺ఆٛ͸ۃྗආ͚Δ ▸ SDKͱ͔Ͱ͸ฏؾͰ࢖ͬͯͨΓ͠·͢ΑͶ…(খ੠) 
 ▸ ֎෦ʹΑΓ؆୯ʹॻ͖׵͑Մೳ ▸ Getter ͷެ։͸OK ▸ Setter ͸ϝιουͰදݱ ▸ τϨʔαϏϦςΟͷ؍఺ΑΓ
 ϓϩύςΟΑΓϝιουͷํ͕Φεεϝ
  6. PUBLICม਺໰୊ PUBLICม਺ͷར༻έʔε ▸ σʔλߏ଄ (ΤϯςΟςΟ) ͷදݱ ▸ ग़དྷΕ͹Getter/Setter ܦ༝͕๬·͍͕͠
 ৔߹ʹΑͬͯ͸ެ։͠ͳ͚Ε͹͍͚ͳ͍͜ͱ΋͋Δ

    ▸ ΠϯλʔϑΣʔεܦ༝΋OK ▸ μϛʔίʔυ/αϯϓϧίʔυ࡞੒࣌ʹ༻͍Δ ▸ ग़དྷΕ͹࢖Θͳ͍ํ͕ྑ͍ ▸ αΫοͱઆ໌͢Δ෼ʹ͸࢖͏͜ͱ΋͋Δ
  7. Ξϯνύλʔϯྫ Public ม਺໰୊ Prefab ͷ SerializeField ໰୊ Awake / Start

    ໰୊ ิؒؔ਺ ໰୊ Shader ໰୊
  8. PUBLICม਺໰୊ ໰୊఺ ͜͜ʹଞऀʹґଘ͍ͯ͠ΔΦϒδΣΫτ͕͋Δ͡ΌΖ

  9. PUBLICม਺໰୊ ໰୊఺ ͜ΕΛPrefabԽͯ͠ಈతੜ੒ʹ͠Α͏ͱ͢Δͱ

  10. PUBLICม਺໰୊ ໰୊఺ ͜͏͡Όʂ

  11. PUBLICม਺໰୊ ໰୊఺ ▸ ԿͰ΋͔ΜͰ΋ࢀরΛInspector Ͱͷઃఆͯ͠͠·͏ ▸ ࣗ਎ͷPrefabҎ֎ͷཁૉ ▸ ґଘؔ܎͕ΊͪΌͪ͘Ό ▸

    ॳظԽʹऑ͍
 ▸ PrefabԽͨ͠ͱ͖ʹґଘ͕ണ͕ΕΔ ▸ ϖλϖλషΔཁૉ͕ଟ͍ͱΞλον΋Ұۤ࿑
  12. PUBLICม਺໰୊ ղܾ๏ ▸ Inspector Ͱొ࿥͢΂͖΋ͷ ▸ ࣗ਎ҎԼͷཁૉ ▸ ಈతੜ੒͢ΔPrefab ▸

    ґଘੑͷղܾ ▸ ͪΌΜͱઃܭ͢Δʂ ▸ Ͳ͏ͯ͠΋ݫ͍͠৔߹͸Zenject ͳͲͷDI ίϯςφΛ༻͍Δ
  13. Ξϯνύλʔϯྫ Public ม਺໰୊ Prefab ͷ SerializeField ໰୊ Awake / Start

    ໰୊ ิؒؔ਺ ໰୊ Shader ໰୊
  14. AWAKE / START ໰୊ ▸ ͱΓ͋͑ͣॳظԽ͸Awake/StartͰߦ͏ ▸ ֤ॴͰݺͼ͗ͯ͢Awake ͷ
 ࣮ߦॱʹґଘੑ͕ੜ͡Δ


    
 
 ▸ ࢖Θͳ͍Awake()/Start()/Update() ▸ ਺͕ଟ͍ͱݺͼग़͠ίετ͕
 അࣛʹͳΒͳ͍(ಛʹUpdate) ໰୊఺
  15. AWAKE / START ໰୊ ▸ ґଘੑͷ͋ΔॳظԽ ▸ ॳظԽؔ਺Λ༻ҙ͢Δ
 
 


    
 
 ▸ ॳظԽॲཧΛ؅ཧ͢Δ
 
 
 ໰୊఺ͷղফ๏
  16. AWAKE / START ໰୊ ▸ ࢖Θͳ͍Awake()/Start()/Update() ▸ ফ͢!! ໰୊఺ͷղফ๏

  17. Ξϯνύλʔϯྫ Public ม਺໰୊ Prefab ͷ SerializeField ໰୊ Awake / Start

    ໰୊ ิؒؔ਺ ໰୊ ໊લۭؒ ໰୊ Shader ໰୊
  18. ิؒؔ਺ ໰୊ ▸ Ґஔߋ৽Ͱ͜Μͳίʔυ͕
 
 
 
 ▸ Lerp/Slerp ͸਺ֶతʹ͸ʮ಺෼఺ʯΛܭࢉ͢Δؔ਺

    ▸ ͔֬ʹdeltaTime ͷׂ߹Ͱগͣͭ͠໨ඪʹۙͮ͘ ▸ ແݶ͔͚࣌ؒͳ͍ͱ࠷ऴ஋ʹ͸ͳΒͳ͍ ▸ ͦ΋ͦ΋0.033/0.016 ͣͭۙͮ͘ॲཧ͸ద੾͔ʁ ▸ ॲཧམͪͳͲ͢Δͱׂۙͮ͘߹΋ಈతʹมΘͬͯ͠·͏ ໰୊఺
  19. ิؒؔ਺ ໰୊ ▸ Lerp ͷ࢖͍ํΛ೺Ѳ͢Δ ▸ ͪΌΜͱυΩϡϝϯτΛಡΈ·͠ΐ͏
 https://docs.unity3d.com/ja/current/ScriptReference/Vector3.Lerp.html
 ▸ ୈࡾҾ਺Λ0.0~1.0

    ʹมԽ͢Δίʔυʹ͢Δ
 ex) Coroutine
 Ͱͷ࣮૷ྫ ղܾ๏
  20. Ξϯνύλʔϯྫ Public ม਺໰୊ Prefab ͷ SerializeField ໰୊ Awake / Start

    ໰୊ ิؒؔ਺ ໰୊ Shader ໰୊
  21. SHADER ໰୊ ▸ ϐΫηϧγΣʔμʔ಺Ͱॲཧͷॏ͍ؔ਺Λ༻͍Δ ▸ ifจ ▸ ৚݅෼ذ͸ॏ͍ॲཧ ▸ ࡾ֯ؔ਺(

    Sin,Cos,Tan ) ▸ ܭࢉࣗମ͕ॏ͍ ▸ Log, Sqrt ͳͲ ໰୊఺
  22. SHADER ໰୊ ▸ ϐΫηϧγΣʔμʔ಺Ͱͷॲཧͷॏ͍ؔ਺ ▸ ifจ ▸ ܰྔͳ਺ֶؔ਺Ͱ୅ΘΓͷදݱ
 ex) ceil(clamp(_value,

    0, 1)) _value ͕ෛ:0, ਖ਼:1
 uvͰv ͕0.5 Ҏ্͔Ͳ͏͔ˠ ceil(clamp(i.uv.y - 0.5, 0, 1)) ▸ ࡾ֯ؔ਺( Sin,Cos,Tan ) ▸ ࡾ֯ؔ਺ςʔϒϧΛ࡞੒/ςΫενϟʹॻ͖ࠐΜͰ͓͘ ▸ r=Sin, g=Cos, b = Tan ▸ Log, Sqrt ͳͲ ▸ ۃྗ࢖Θͳ͍ ▸ Sqrt ͷ୅ΘΓʹೋ৐ͰൺֱͳͲ ղফ๏
  23. ࠷ޙʹ ▸ υΩϡϝϯτ͸ͪΌΜͱಡΈ·͠ΐ͏ ▸ Θ͔Βͳ͍͚Ͳ࢖ͬͯΈͨ͸NG
 ▸ ઃܭΛͪΌΜͱߟ͑·͠ΐ͏ ▸ ಛʹӡ༻ίϯςϯπͳͲ͸ޙʑͰͬ͠΃ฦ͕͖͠·͢ ▸

    ٕज़తෛ࠴ͱ޲͖߹͍·͠ΐ͏ ▸ ύϑΥʔϚϯεΛؾʹ͠·͠ΐ͏ ▸ ৭ʑݕূهࣄͳͲΛ୳ͯ͠Έ·͠ΐ͏ ▸ ͳ͚Ε͹ࣗ෼Ͱܭଌͯ͠Έ·͠ΐ͏ ▸ ग़དྷΕ͹ͦͷ݁ՌΛγΣΞͯ͠΋Β͑Δͱ
 ΈΜͳ͕޾ͤʹͳΓ·͢ ·ͱΊ