Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by dCalle · Jan 29, 2017 at 03:09 PM · prefabsnavigationsearchassign-variablederived-class

Narrow Prefab Selection through attached Scripts

Hi!


I just edited my ObjectPool Script that takes in Scripts derived from the abstract PoolItem Class. (or however you would call that)

Sadly when I want to assign the Prefabs I used before, I can't even find a single Object to take in, although I have at least 2 Prefabs that have a PoolItem derived Script attached.

Isn't there a Way to make the Editor look for GameObjects that only have a required Script attached?

Or would that at least be something people need? Wha do you think about that?

Guess I have to rollback and assign GameObjects from the huge cluster of Prefabs in the Selection Window, and then log something in the OnValidate method. But my approach feels ...better... to me^^


Someone out there able to help?


Have a nice time coding ;-)

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 Glurth · Jan 29, 2017 at 05:11 PM

In your own code you can just check that gameObject.GetComponent< particularScriptClass >() does not return null.

Regarding the editor component; it looks to me, like this function DOES allow you specify a particular type for the selected object, the second parameter. Have you tried this?

 particularScriptClassObject=EditorGUILayout.ObjectField(particularScriptClassObject, typeof( particularScriptClass), false);

https://docs.unity3d.com/ScriptReference/EditorGUILayout.ObjectField.html

Comment
Add comment · Show 2 · 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 dCalle · Jan 29, 2017 at 10:43 PM 0
Share

hmm, i tried that, but it doesn't help at all...

All I wanted to say: How can I filter, in the GameObject Selection Window, GameObjects that only have a certain Script attached?

avatar image Glurth dCalle · Jan 29, 2017 at 10:55 PM 0
Share

Surprised that didn't work. $$anonymous$$eep in $$anonymous$$d the particularScriptClassObject variable is of type particularScriptClass. If you want the GameObject this script is attached to, it will be accessible via particularScriptClassObject.gameObject

avatar image
0

Answer by dCalle · Jan 29, 2017 at 11:21 PM

Well, all I did, was replacing GameObjects that are taken in with the PoolItem Script. I eventaully had to figure, that Unity doesn't check the Asset GameObjects for their Scripts, which kind of was a pain in the ass... I kinda do relaize i need something like github now^^

I think its a bit more complex than you think ;-)

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [Serializable]
 public class ObjectPool
 {
 
     public GameObject           prefab;
 
     private string              identifier = "";
 
     public bool                 grows = true,
                                 found;
 
     public int                  curItem     = 0,
                                 lastObject  = 0,
                                 max         = 0;
 
 
 
     [ReadOnly] public List<GameObject>         pool            = new List<GameObject>();
     [ReadOnly] public LinkedList<PoolItem>     recyclingPool   = new LinkedList<PoolItem>();
 
 
 
     public ObjectPool(GameObject prefab, int amount)
     {
         ValidateObject(prefab);
         this.prefab = prefab;
         AddObjects(amount);
     }
 
     private void ValidateObject(GameObject prefab)
     {
         if (!prefab.GetComponent<PoolItem>())
         {
             Debug.Log(prefab.name + ": Attaching PoolItem");
             prefab.AddComponent<PoolItem>();
         }
     }
 
     public void AddObjects(int amount)
     {
         for (int i = 0; i < amount; i++)
         {
             GameObject obj = CreatePrefab();
             obj.name += identifier + " " + i.ToString();
             pool.Add(obj);
         }
     }
 
     private GameObject CreatePrefab()
     {
         GameObject obj = MonoBehaviour.Instantiate(prefab);
         obj.SetActive(false);
         return obj;
     }
 
     public GameObject GetNext(out bool needsRecycling)
     {
         GameObject ret = null;
         found = false;
         needsRecycling = false;
         while (!found)
         {
             if (recyclingPool.Count > 0)
             {
                 PoolItem item =  recyclingPool.First.Value;
 
                 item.CancelInvoke("Disable");
                 needsRecycling = true;
 
                 ret = item.gameObject;
                 recyclingPool.RemoveFirst();
             }
             else if (!pool[curItem].gameObject.activeInHierarchy)
             {
                 ret = pool[curItem];
                 lastObject = curItem;
 
                 IncrementNext();
                 found = true;
             }
             else
             {
                 IncrementNext();
                 if (lastObject == curItem)
                 {
                     if (grows)
                     {
                         ret = CreatePrefab();
                         pool.Add(ret);
 
                         IncrementNext();
                         lastObject = curItem;
                         found = true;
 
                     }
                     else
                         break;
                 }
             }
         }
         return ret;
     }
 
     private void IncrementNext()
     {
         curItem++;
         if (curItem >= pool.Count)
         {
             curItem = 0;
         }
     }
 
     public void RefreshPrefabs(GameObject newPrefab = null)
     {
         if (newPrefab)
         {
             prefab = newPrefab;
         }
         for (int i = 0; i < pool.Count; i++)
         {
             GameObject oldPrefab = pool[i];
             pool[i] = CreatePrefab();
             MonoBehaviour.Destroy(oldPrefab);
         }
     }
 }


I wanted to avoid the enabling/disabling of Gameobjects, because they turn out to be very cost-intensive when enabling/disabling hundreds in just a few seconds (bullethell). So I decided to give the used gameobjects a time span, where they can be recycled, without disabling and enabling them. I figured it would also make much more sense to have one big pool manager that pools as many objects of the same class as possible, to maximize the recycling rate.

Actually, I'm still on it, and it would have been easier to give all the scripts that are pooling the PoolItem Component of the going-to-be-pooled objects.

Thanks for your help anyways. Am very grateful for the community feeling ;-)

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to instantiate prefabs at certain times? 1 Answer

Why the prefabs loose their preview in the Project windows ? 2 Answers

Instantiating without the use of Resource Folder in Unity? 0 Answers

How to generate object (Coins) between two object clones (obstacles) 0 Answers

Is there a solution for the waypoint prefab issue? 2 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