Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Code and workflow for hiding/unhiding unity elements

Code and workflow for hiding/unhiding unity elements

View previous topic View next topic Go down  Message [Page 1 of 1]

Kaivis


Member [Lv. 2]
Member [Lv. 2]
Hi Everyone!

I hope I am posting in the correct section. I know that it is possible to write a custom code in Unity so I have the following questions:
1. Imagine a house model in Unity. Would it be possible to have a code which helps to hide/unhide certain objects? For example, letter "W" would hide/unhide all windows, letter "C"  would hide/unhide all columns etc.
2. If it would be possible to develop a code for that, what would be the workflow? How would Unity know what is window and what is door?
3. Taking one step further.. Would it be possible to have a code that unhides the next step of the project. For example, the first step would be building foundations. Would it be possible to have a code that would unhide the next step, say, 1st floor floor element, with a klick of a keyboard key? And then with the same key unhide the next step which might be 1st floor walls. And would it be possible with another key go backwards?
4. If such code would be possible, what do you think would be the workflow? How would Unity know which element is in which step?

I am keen to see if this is possible!

View user profile
Firstly you are looking at it in a linear view, when you say how would unity classify what object is what.. you are over thinking things. The engine will precisely follow the commands given, therefore it does not have a brain to interpret.. So In terms of toggling a gameobject's renderer, you have to set the gameobject's renderer.enabled property to either true or false, something like this:
Code:
function Update() {
 
    if (Input.GetKeyDown(KeyCode.W)) {
        // show (in this case windows)
        renderer.enabled = true;
    }
 
    if (Input.GetKeyDown(KeyCode.E)) {
        // hide Window with keypress E
        renderer.enabled = false;
    }
}
Now this code would be implemented onto the gameobject you would like to have this toggle effect on, however if you wanted to toggle the windows with only "W" you could do this:
Code:
function Update() {
 
    if (Input.GetKeyDown(KeyCode.W)) {
        // (toggle visibility:)
        renderer.enabled = !renderer.enabled;
    }
}

However like you mentioned the columns, I am assuming there are multiple columns, therefore you must create a parent column and make the rest a child of the parent and use this code to toggle all the columns at once:
Code:
function Update() {
    if (Input.GetKeyDown(KeyCode.C)) {
        ToggleVisibility();
    }
}
 
function ToggleVisibility() {
    // toggles the visibility of this column and all it's children
    var renderers = gameObject.GetComponentsInChildren.();
    for (var r : Renderer in renderers) {
        r.enabled = !r.enabled;
    }
}

View user profile http://www.youtube.com/user/misterninjaboy

Kaivis


Member [Lv. 2]
Member [Lv. 2]
Hey MasterNinjaBoy,

Thank you so much for your input!

The model would be imported from 3DS Max so I will not have an option to create columns my self. All the model elements would be imported from 3DS Max. Does it mean that I would have to click on every single element and assign the proper toggle visibility code?

How about the question 3? Do you have an opinion on that?

View user profile

View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum