Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 wiiarethesound · Oct 04, 2012 at 03:56 AM · editorprefabtypereplace

Determine a prefab's "type"?

I am trying to create an Editor utility that will let me replace all the prefabs of a certain type in a parent object with a different prefab. The big problem I am running into is that I can't seem to find a way of determining what "type" a prefab is. Here is what I have so far. The problem part is the loop in ReplacePrefabs():

 public class ReplacePrefabEditor : EditorWindow
 {
     Object removalObject;
     Object replacementObject;
 
     void OnGUI()
     {
         EditorGUILayout.BeginVertical();
         removalObject = EditorGUILayout.ObjectField("Removal Object", removalObject, typeof(Object), true, null);
         replacementObject = EditorGUILayout.ObjectField("Replacement Object", replacementObject, typeof(Object), true, null);
         EditorGUILayout.EndVertical();
 
         if (GUILayout.Button("Replace"))
         {
             if (!removalObject || !replacementObject)
             {
                 Debug.LogError("Need to assign objects!");
                 return;
             }
             ReplacePrefabs();
         }
     }
 
     [MenuItem("Prefab Helpers/Replace Prefabs")]
     static void Init()
     {
         ReplacePrefabEditor window = (ReplacePrefabEditor)EditorWindow.GetWindow(typeof(ReplacePrefabEditor));
     }
 
     [MenuItem("Prefab Helpers/Replace Prefabs", true)]
     static bool ValidateSelection()
     {
         return Selection.activeTransform != null;
     }
 
     void ReplacePrefabs()
     {
         Transform root = Selection.activeTransform;
         int childCount = root.childCount;
         for (int i = 0; i < childCount; ++i)
         {
             // determine if the current child is the same prefab type as removalObject
         }
     }
 }
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by MarkFinn · Oct 04, 2012 at 06:17 AM

There's probably a better way, but as I need version control on my assets (Check version against server list of current versions) I use a very simple script for this. Just create a version of the script below and apply it to the assets.

 using UnityEngine;
 
 public class Version : MonoBehaviour {
     public string type;
     public int version;
     public string Artist;
     public string Source;
 }
 
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 JanWosnitza · Oct 04, 2012 at 03:38 PM

This should do it:

 void ReplacePrefabs()
 {
     var root = Selection.activeTransform;
     var childrenToReplace = new List<Transform>();

     // first collect children to replace
     // (don't change a collection while enumerating over it!)
     foreach ( Transform child in root )
     {
         if ( child.gameObject == removalObject )
             childrenToReplace.Add( child );
     }

     // replace children
     foreach ( var childToReplace in childrenToReplace )
     {
         var instance = Object.Instantiate( replacementObject );
         instance.name = childToReplace.name;
 
         Object.DestroyImmediate( childToReplace );
     }
 }
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

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

12 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

Related Questions

Reference to Scene Object Component in Prefab Editor, 'Type Mismatch'? 1 Answer

What is the type of my prefab? 2 Answers

Determine if game object is tyoepf One Prefab 3 Answers

The Script Equivilent of dragging a hierarchy of meshes over a preexisting prefab? 0 Answers

Why do prefabbed meshes go missing whenever I pull an update from Unity Collab? 0 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