Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xtro · Aug 30, 2017 at 06:17 PM · prefabassetdatabaseprefabutility

How to check if an object is asset in build runtime (Not in Editor) ?

Ok. This has been asked many times in different formats. I thought I made it work with some kind of implementation. After years of experimenting, I still don't have a perfectly working solution.

I REALLY need to know if an object is a prefab/asset or it's a scene object(or created at runtime) and I need this info in the build runtime. Not in Editor (via UnityEditor namespace).

I am looking for a method similar to these and can be used in build runtime:

  • UnityEditor.AssetDatabase.Contains()

  • UnityEditor.PrefabUtility.GetPrefabType() == PrefabType.Prefab

Question: Does anyone have any idea about how to get this kind of information in build runtime?

For those who want to know why I need this: I'm the developer of a 3rd class library which can send a message (a method call) to some scripts (I call them receivers) who belong to the user of my library and I want to exclude the prefabs from receiving that message. The message must be sent only to the scene objects (or objects created at runtime). Since those objects are not part of my library, I can't keep track of instantiations of them.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zereda-games · Feb 20, 2019 at 02:55 PM 0
Share

I wanna fav this page, how do i do that? I've been workin with Unity for the past 3-4 years now, Ive build many things but never actually released anything as of yet. my goal is large, and time consu$$anonymous$$g. There are some details i would like to reference back to.

Thanks.

avatar image Xtro zereda-games · Feb 20, 2019 at 03:01 PM 0
Share

There is a start shaped button under "up vote" and "down vote" buttons on the left side of the question.

4 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Xtro · Feb 20, 2019 at 02:48 PM

I have been using this extension method for long time now. Maybe I should I it here as an answer:

 public static class TransformExtensions
 {
     public static bool IsPrefab(this Transform This) => !This.gameObject.scene.IsValid();
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by FortisVenaliter · Aug 31, 2017 at 09:25 PM

Nope, not possible natively. Once a scene starts, there basically are no prefabs in it. Everything becomes it's own instance with no prefab connection. That information simply isn't needed by the engine at that point.

But... there is one way you might be able to finagle it... As far as I know, the Start() function is never called on prefabs. Only on active scripts on active objects in an active scene. So, you could set a flag in Start() saying it's okay to receive these messages. Unfortunately, that would preclude your messages from being sent to objects that have been inactive since the start of the scene.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xtro · Aug 31, 2017 at 10:56 PM 0
Share

I am not talking about prefabs added to the scene manually in the editor. I am talking about the actual prefab objects waiting in the memory just to be instantiated at a random point in time.

Example: Car is a prefab in the project. You have a reference to Car prefab in Street scene. A script in Street scene waits user input then instantiate Car prefab with a new name like PlayerCar or EnemyCar. Until the script instantiated PlayerCar and EnemyCar objects using Car prefab, the Car prefab was waiting in the memory doing nothing.

What I want to do in my class library is: Get a list of cars from the programmer. Send a message to the cars in the list by skipping prefabs. So PlayerCar and EnemyCar would receive the message and Car wouldn't.

Since I am developing a 3rd party library, I don't have any control about the scripts attached to car objects and I don't know what's included in the list co$$anonymous$$g from the programmer.

avatar image
0

Answer by Bunny83 · Sep 01, 2017 at 01:12 AM

Most what @FortisVenaliter said in his answer is correct. Conceptionally the only difference between a gameobject in the scene and a prefab is that the prefab is not added to any rendering / processing lists. So they don't get any callbacks and are considered as "not part of the scene". Besides that there's no difference between an instance and a prefab. At runtime there is no assetdatabase. It's only required / used by the editor.

The AssetDatabase is just a management tool of the editor that tracks all assets in a project. However in a build Unity only includes used / referenced things.

The only thing you can check is if a GameObject is part of any scene or not. Since the introduction of the new SceneManager every GameObject has a scene property. Note that this property seems to always return a Scene object. You have to check it's IsValid() method to determine is this object belongs to a scene or not.

Note dynamically instantiated objects usually have a negative instance ID. Serialized objects have a positive ID. Dynamically created / instantiated objects do belong to the current loaded scene when you check it's scene property.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image unityBerserker · Sep 01, 2017 at 08:45 AM 0
Share
 GameObject[] allFoundGameObjects = Resources.FindObjectsOfTypeAll (typeof(GameObject)) as GameObject[];
             foreach (var item in allFoundGameObjects) {
                 if (item.hideFlags == HideFlags.None && item.scene.rootCount > 0) {
                     //we find all GameObjects in hierarchy
                 }
                 if (item.hideFlags == HideFlags.None && item.scene.rootCount == 0) {
                     // we find all prefabs
                 }
             }
     
     
 
avatar image Xtro · Sep 01, 2017 at 12:10 PM 0
Share

IsValid() and negative instance ID thing sounds useful. I'll try those.

avatar image Firestar9114 · Feb 20, 2019 at 10:55 AM 0
Share

Scene.IsValid() gives inconsistent results, at least in 2018.3 in my testing.

avatar image Xtro Firestar9114 · Feb 20, 2019 at 02:53 PM 0
Share

I suspect that Scene.IsValid may return true when in prefab edit mode. But even if that's the case, my reason of using it as a prefab check is for runtime only and in the runtime, prefab edit mode is not a valid case.

$$anonymous$$aybe I should test it on 2018.3 properly.

Thank you for your comment.

avatar image
0

Answer by Firestar9114 · Feb 20, 2019 at 10:52 AM

So this seems to do the trick in 2018.3: PrefabStageUtility.GetPrefabStage() unfortunately, it is listed as experimental and subject to change... https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Experimental.SceneManagement.PrefabStageUtility.GetPrefabStage.html You can then use this to update a boolean in editor-scripting prior to runtime, and check that bool safely at runtime.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Feb 20, 2019 at 10:57 AM 0
Share

Uhm, the namespace is UnityEditor.Experimental.Scene$$anonymous$$anagement. So it's still just an editor class which can't be used at runtime. Note that when you test inside the editor, the editor functionality is available. However when you build your game you will get errors.

avatar image Firestar9114 Bunny83 · Feb 20, 2019 at 11:03 AM 0
Share

Yes, but you can set a boolean in the editor prior to run-time, which can be safely checked at run-time. I forgot to mention this initially and updated my original comment.

avatar image Bunny83 Firestar9114 · Feb 20, 2019 at 01:36 PM 1
Share

Well, the question was specifically to test if an object reference is a prefab or an instance at runtime. The PrefabUtility exists for along time which could already be used at edit time. The OP has actually mentioned it in the question. However this is also an editor class.


Setting a boolean doesn't help much. If you store that boolean on the prefab itself it would be true in instances as well after calling Instantiate. You would need to store that information in a central place. However in this case you can simply manually manage your prefabs by dragging them into an array.


Actually i never was in need to identify an arbitrary object as prefab since you usually track your prefab references in some way. Also in most cases you track your instances as well.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can anyone help with creating Prefabs for procedural meshes 1 Answer

cannot load GameObject Prefabs using Resources.Load in Android 0 Answers

Getting Prefab Icon from AssetDatabase 0 Answers

Unity 4 PrefabUtility.InstantiatePrefab problem in OnPostprocessModel 0 Answers

PrefabUtility - Check if Changes have been made to Prefab? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges