Search Unity

Unity 4.6 Model Viewer

Discussion in 'Scripting' started by Studio_Akiba, Jan 28, 2015.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,423
    A few weeks ago someone wrote a basic model viewing script for me, I am trying to convert it to work with the Unity 4.6 GUI to make things much easier to do, with little luck, I am hoping someone here can help me with this, I am intenting on releasing this so others can have an easy way to make interactive model viewers/showreel's for their work.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Linq;
    5.  
    6. public class ModelViewer : MonoBehaviour
    7. {
    8.     [System.Serializable]
    9.     public class ModelViewerInformation{
    10.         public GameObject Model;
    11.         public List<AnimationClip> Animations;
    12.     }
    13.    
    14.     public ModelViewerInformation[] Models;
    15.    
    16.     public int ModelIndex;
    17.     void UpdateModelViewer(){
    18.         //        Transform[] childs = GetComponentsInChildren<Transform>();
    19.         for(int i=0; i< transform.childCount; i++)
    20.         {
    21.             Destroy (transform.GetChild(i).gameObject);
    22.         }
    23.        
    24.        
    25.        
    26.         GameObject SpawnedModel = Instantiate(Models[ModelIndex].Model, transform.position, transform.rotation) as GameObject;
    27.         SpawnedModel.transform.parent = transform;
    28.        
    29.     }
    30.    
    31.     void Update(){
    32.         if (Input.GetKeyDown(KeyCode.LeftArrow)){
    33.            
    34.             if (ModelIndex > 0) {
    35.                 ModelIndex -= 1;
    36.             } else {
    37.                 ModelIndex = Models.Length;
    38.             }
    39.             UpdateModelViewer();
    40.         }
    41.         if (Input.GetKeyDown(KeyCode.RightArrow)){
    42.            
    43.             if (ModelIndex < Models.Length) {
    44.                 ModelIndex += 1;
    45.             } else {
    46.                 ModelIndex = 0;
    47.             }
    48.             UpdateModelViewer();
    49.         }
    50.        
    51.        
    52.     }
    53. }
     
    Last edited: Jan 28, 2015
    twobob likes this.
  2. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    2,058
    Okay so I will jump in here.

    What is it you want to do?
    And what is LINQ used for in your declarations?

    If nothing ditch it.

    I'm booting up your code now.

    EDIT:

    First suggestion:
    Code (csharp):
    1.  
    2. ModelIndex = Models.Length;
    3.  
    should be
    Code (csharp):
    1.  
    2. ModelIndex = Models.Length -1;
    3.  
    as it is a zero index system

    and so it should also be
    Code (csharp):
    1.  
    2. if (ModelIndex < Models.Length -1)
    3. {
    4. ModelIndex += 1;
    5. }
    6.  
    that will stop the Array out of Range errors at least.

    Other than that I see no issues with this on 4.6 ViewedModel.JPG

    and a

    Code (csharp):
    1.  
    2. void Start() {
    3. // will default to zero or the inspector value
    4. UpdateModelViewer();
    5. }
    6.  
    wouldn't hurt neither (tested on 4.6.1ish)
    version.JPG
     
    Last edited: Jan 29, 2015
  3. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    2,058
    hookedUpVersion.4.6.JPG

    here it is with buttons, fancy rotation of model and lights with thrown-together limits system, shadow control, a moderately sane setup of positions and almost no error checking yay.

    Enjoy.

    Did I mention the almost illegible FPS counter? do I not give and give.

    (the public domain santa steve is available online... I do not claim ownership, the arguably similar UV design of said thing to a popular gamestyle likewise is not enforced as owned by Mojang (or now MS), And we all lived happily ever after, the end)

    Hint to next annoyed poster about shoddy demos, simply cache a starting offset to constrain the Absolute Y rotation relative to the starting position and compare that instead of the current Euler.
     

    Attached Files:

    Last edited: Mar 27, 2015
  4. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    2,058
    and here is a version that just elides the animations reference (for simplicity here)

    and runs through the embedded animations (with annotated caveats)

    This seems the most likely use-case so I have included it.
    Here it is in action
    http://kaycare.co.uk/games/ModelViewer/

    Here is the updated code
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class ModelViewer : MonoBehaviour
    7. {
    8. [System.Serializable]
    9. public class ModelViewerInformation
    10. {
    11. public GameObject Model;
    12. }
    13.  
    14. public ModelViewerInformation[] Models;
    15.  
    16. public int ModelIndex;
    17. void UpdateModelViewer()
    18. {
    19. for (int i = 0; i < transform.childCount; i++)
    20. { Destroy(transform.GetChild(i).gameObject); }
    21.  
    22. GameObject SpawnedModel = Instantiate(Models[ModelIndex].Model, transform.position, transform.rotation) as GameObject;
    23. SpawnedModel.transform.parent = transform;
    24.  
    25. UpdateAnim(ref SpawnedModel);
    26.  
    27. }
    28.  
    29.  
    30. public void LoadPrevious() {
    31.  
    32. if (ModelIndex > 0)
    33. {
    34. ModelIndex -= 1;
    35. }
    36. else
    37. {
    38. ModelIndex = Models.Length - 1;
    39. }
    40. UpdateModelViewer();
    41. }
    42.  
    43.  
    44. public void LoadNext() {
    45.  
    46.  
    47. if (ModelIndex < Models.Length - 1)
    48. {
    49. ModelIndex += 1;
    50. }
    51. else
    52. {
    53. ModelIndex = 0;
    54. }
    55. UpdateModelViewer();
    56. }
    57.  
    58. // I skip the cruft default zero one. This could be an option.
    59. private int currentAnimationIndex = 1;
    60.  
    61. void Update()
    62. {
    63. if (anim)
    64. {
    65. if (!anim.isPlaying)
    66. {
    67. // we always just step over the index forwards. so, easy test.
    68. currentAnimationIndex ++;
    69. if (currentAnimationIndex >= animations.Count)
    70. {
    71. currentAnimationIndex = 1;
    72. }
    73. anim.Play(animations[currentAnimationIndex]);
    74. anim.wrapMode = WrapMode.Once;
    75. }
    76. }
    77.  
    78. // Old stuff
    79. if (Input.GetKeyDown(KeyCode.LeftArrow))
    80. { LoadPrevious(); }
    81. if (Input.GetKeyDown(KeyCode.RightArrow))
    82. { LoadNext(); }
    83.  
    84. }
    85.  
    86. List<string> animations = new List<string>();
    87. Animation anim;
    88.  
    89. void UpdateAnim(ref GameObject model)
    90. {
    91.  
    92. anim = model.GetComponent<Animation>();
    93. animations.Clear();
    94.  
    95. // horrid
    96. foreach (AnimationState state in anim) { animations.Add(state.name); }
    97.  
    98. // now we have a list we could create buttons "on the fly" with each name...
    99. // and call up the animation like anim.Play(animations[ValueWeAddedForThisButton]);
    100. // or just go for one and scroll through them...
    101. // often the first animation is junk so perhaps we should add a Skip Root Anim bool...
    102.  
    103. if (animations.Count > 0)
    104. {
    105. // we reset the currentAnimationIndex to 1
    106. currentAnimationIndex = 1;
    107. anim.Play(animations[currentAnimationIndex]);
    108. }
    109. anim.wrapMode = WrapMode.Once;
    110. }
    111.  
    112. void Start() {
    113. // the model index played will default to zero or possibly the inspector value
    114. UpdateModelViewer();
    115. }
    116. }
    117.  
    118.  
    Here are some screenies
    bear.JPG
    Stag.JPG
    bear2.JPG stag2.JPG
    I made all the values to bind to to stop rotation. lights moving, blah blah nice and clear to be linked to GUI system via buttons. You can note the example in the buttons I used above.

    I was not even moderately careful with design considerations, frankly a small child could improve on it.

    Henjoy
     

    Attached Files:

    Last edited: Jan 29, 2015
  5. twobob

    twobob

    Joined:
    Jun 28, 2014
    Posts:
    2,058
    Attach the field of view value from the camera to your sliders Computed Value Float for a "zoomer".

    sliderZoom.JPG

    FOV.JPG
     
    Last edited: Jan 29, 2015