how to clone several game objects in a way that clone properties of one can be adjusted to match all others in scene view

后端 未结 8 871
执念已碎
执念已碎 2020-12-11 15:29

I asked How can I adjust shape/dimensions of one clone to affect all other clones in the scene view and the accepted answer was spot on. It could only clone one game object.

相关标签:
8条回答
  • 2020-12-11 15:59

    Create script CopycatManager that will hold a leader and then use dedicated setters for copying the other object properties that have the same type. If a property is a default one may need to set up either a proxy of such property within' the script or play with triggers. I would recommend proxy. Like this:

    class CopycatManager {
    
        public GameObject leader;
    
        SomeAttributeType attributeToCopyFromLeader {get; private set}
    
        void Start () {
            // The first CopycatManager to start is the leader
            List<CopycatManager> allCMs = parent.GetComponentsInChildren();
            CopycatManager foundLeader = allCMs.Find(o => o.leader == o);
            if (foundLeader == null) {
                // There's no leader yet, set yourself a leader
                leader = this;
            } else {
                // Found a leader, accept
                leader = foundLeader;
            }
        }
    
        public void SetAttribute (SomeAttributeType newVal) {
            // If we're setting the attribute of the leader - we should set this attribute for all children
            if (leader == gameObject) {
                // Find all copycat manager scripts attached to children of current parent
                // Meaning siblings
                // WARNING: It will include children of siblings and the leader itself
                // WARNING: It will not include parents of the Copycat Manager type, add if required
                List<CopycatManager> allCMs = parent.GetComponentsInChildren();
                foreach (CopycatManager manager in allCMs) {
                    SetAttributeFromLeader (newVal);
                }
            } else {
                // Non-leader is attempting to change attribute - call leader
                leader.SetAttribute(newVal);
            }
        }
    
        // Called by leader to each child
        public void SetAttributeFromLeader (SomeAttributeType newVal) {
            attributeToCopyFromLeader = newVal;
        }
    }
    

    Make sure to assign a new leader if the old one destroyed. Only destroy objects with CopycatManager through dedicated function.

    0 讨论(0)
  • 2020-12-11 16:00

    use a singleton class. add that script to all the objects, then you can make a call to one and it will adjust all of them.

    you can also do this with a static class, but the singleton approach is cleaner, and gives you more options.

    public class MySingleton
    {
    
        private static MySingleton fetch; // keep the static reference private
    
        public bool myBool = false;
    
        // and expose static members through properties
        // this way, you have a lot more control over what is actually being sent out.
        public static bool MyBool { get { return fetch ? fetch.myBool : false; } }
    
        void Awake()
        {
            fetch = this;
        }
    }
    

    read here for some great information on both options!

    0 讨论(0)
提交回复
热议问题