"Unity Practical Skills" Unity Editor Tool loading prefabrication (Prefab) and scenes (scene)

Source: Internet
Author: User

It's often necessary to pull out the prefab or open the scene for testing when you're packing in unity or helping to plan your art.

In fact, this is in the UI packaging in the article mentioned, but the focus is different, the focus of the article is packaging, the focus of this is to put a little bit of knowledge inside out to talk about

And then we'll talk about how they're done separately.

First, pull out the prefabricated prefab

It's like we usually pull the prefab into this place.

1. First is to get the prefab you selected (if there are other requirements, such as all the files in the default directory, etc., do not do so, but similar)

GameObject[] selectGameObjects = Selection.gameObjects;   

Since it's an array, just foreach.

2. Get the resource path through the Assetdatabase interface and determine if the selected file is the specified suffix file under the target folder

string path = AssetDatabase.GetAssetPath(selectGO);  if(path.StartsWith("Resources/XXX"))  {      if(path.EndsWith(".prefab"))      {      }  }  

Confirm Error Start Next

3. Pull into the hierarchy.

GameObject uiInstance = PrefabUtility.InstantiatePrefab(selGb) as GameObject  

It's like pulling into the window.

You can start a variety of operations

Here is a reminder of a similar interface

Prefabutility.createprefab
This is the gameobject generated prefab on the hard drive, which is the opposite of the process.

4. Deleting an instance

The above example is uiinstance, so to delete this instance, that is, to delete from the hierarchy

UnityEngine.Object.DestroyImmediate(uiInstance);  

Update prefab (equivalent to point an apply)

PrefabUtility.ReplacePrefab(instance,go,ReplacePrefabOptions.ConnectToPrefab);  
Second, open the scene

Sometimes you need to calibrate or check resources to finish the scene, you need a program to help write a tool, you need to open the scene

1. Get the selected scene as above

Object[] selectGameObjects = Selection.objects;  

What's different here is that you get an object instead of a gameobject.
2. Also get the scene path and detect the name

string path = AssetDatabase.GetAssetPath(selectGO);  if(path.StartsWith("Resources/XXX"))  {      if(path.EndsWith(".scene"))      {      }  }  

Similarly, you need to change the detection suffix named scene

3. Open scenes scene

Here is the main difference, with the interface is not the same

EditorSceneManager.OpenScene(selGbPath, OpenSceneMode.Single);  Scene currScene = EditorSceneManager.GetSceneByPath(selGbPath);  

As you can see, open the scene first and then get the scene through the path interface.
These two steps are indispensable

4. Do the actions you want

You have to open a scene to check something inside or to modify some of the values in the Gameobject.

In fact, after you call Editorscenemanager.openscene (Selgbpath, Openscenemode.single), your current scene is already the scene you want.

So you need to find your gameobject only gameobject go = Gameobject.find ("name");

5. Save the scene

If you don't need to do anything to open your scene, you don't even have to use scene Currscene = Editorscenemanager.getscenebypath (Selgbpath) in the third step.

To get the scene.

But after all, you're usually not so bored. Only the open does not do anything, so the second step still needs to be acquired.

and then call

EditorSceneManager.SaveScene(currScene);  

So it's done.

There is no need to delete the same as the above instantiation Gameobject, the scene opens to open, but also opens a

Unity practical skills, Unity editor tools, loading prefabrication (Prefab) and scenes (scene)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.