Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Dynamic Model Viewer

Discussion in 'Scripting' started by ATLAS-INTERACTIVE, Jan 14, 2015.

  1. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I am looking for a model viewer I can quickly plug models (and animations) into, I have been looking into this, but cannot find a download for it anywhere:


    If anyone knows of something this easy to use, or where I can get this one from, please let me know.

    Thanks
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    This functionality is already built in to Unity
    Get into the mecanim workflow and you'll see
    Suggest:
     
  3. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Sorry if I seemed a little hard to understand, I know how to use Macanim.
    I don't know how to script a model viewer, I know how to trigger animations, just not how to get the model switching functionality.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    If you are really wanting a runtime solution, just Destroy the current model, and instance the next
     
  5. jabez

    jabez

    Joined:
    Nov 2, 2013
    Posts:
    26
    Okay i started on one for you but stopped at the animations since you said you already know how to do that.
    this is really rough but at least it's something, You can add GUI instead of using arrow keys to change models.
    *BTW ATTACH THIS TO AN EMPTY GAMEOBJECT*
    Also I could make a camera system for you if you wanted me to..
    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 14, 2015
  6. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    FYI your loop 19-21 wont work when there's more than 1 child of the object (perhaps unlikely in this scenario) - you modify the number of children each iteration, and about half would be ignored
     
  7. jabez

    jabez

    Joined:
    Nov 2, 2013
    Posts:
    26
    I see but, there should only be one child within the root of the main game object at all time & that's the model, within the model is everything else, You delete the model you delete everything.
     
  8. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    A camera system could be very useful, how would I add an on-screen GUI to this & could I use the 4.6 GUI?
    Sorry, i'm a 3D artist and not used to scripting, I can trigger things with colliders, that's about it.
     
  9. jabez

    jabez

    Joined:
    Nov 2, 2013
    Posts:
    26
    this is where i become useless i haven't been using unity since the new update & i should but i haven't had time, i come on here to help people if & when i can but if you're good with the new gui replace getkeydown with a guibutton or something if they're even in the new gui. i can start on a camera system for you if you want me to but what controls do you want? Mouse orbit? controlled with numpad?
     
  10. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Very similar controls to the Unity editor:
    left click hold to orbit
    wheel to zoom
    right mouse button to pan (up and down)
    s
    Not sure how easy this is, would it be possible to have the camera move closer or further away depending on the size of the object, or even to resize the object in question so it fits in say a 3x3x3 area upon loading? the latter is probably better
     
  11. ATLAS-INTERACTIVE

    ATLAS-INTERACTIVE

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    Hi, just thought i'd see how this is going, I tried to create my own camera controller, didn't go well.