Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Trying to make a Custom Gameobject

Discussion in 'Scripting' started by Rangoric, Oct 23, 2014.

  1. Rangoric

    Rangoric

    Joined:
    Aug 17, 2014
    Posts:
    12
    Background:
    I am making a Visual Novel, and am using the Hierarchy to store all the data for it.

    So I have:

    The Play
    Scene I
    Line 1
    Line 2
    Choice
    Scene II
    Line 1
    Scene III
    Line 1

    What I want to be able to do is to be able to use the "Create" menu on the Hierarchy to create a new scene, or a new Line. Right now, I create a new empty gameobject, then drag the script onto it. This is a pain. Ideally I would want to hit Create and have a few custom options right there. Or even be able to keyboard shortcut it later on.
     
  2. jabez

    jabez

    Joined:
    Nov 2, 2013
    Posts:
    26
    Haiiiii, a bit late i know but heres a basic script that extends that menu & adds a item to it, It adds a light when you create the gameobject & parents it to the selected gameobject in the Hierarchy.
    replace the light with your script & see how it goes.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using System.Collections;
    4.  
    5.  
    6. public class MenuExtender : ScriptableWizard
    7. {
    8.  
    9.     public enum Options
    10.     {
    11.         Line,
    12.         Scene,
    13.     }
    14.  
    15.  
    16.  
    17.  
    18.     public Options Option;
    19.     public string Info = "line";
    20.  
    21.  
    22.     [MenuItem("GameObject/Create Other/Book")]
    23.     static void CreateWizard()
    24.     {
    25.  
    26.         ScriptableWizard.DisplayWizard("Create Book",typeof(MenuExtender));
    27.     }
    28.  
    29.  
    30.     void OnWizardUpdate()
    31.     {
    32.  
    33.     }
    34.  
    35.  
    36.     void OnWizardCreate()
    37.     {
    38.         GameObject Holder = new GameObject();
    39.         Light light =  Holder.AddComponent<Light> ();
    40.         light.intensity = 1f;
    41.         light.color = Color.blue;
    42.         Holder.transform.parent = Selection.activeGameObject.transform;
    43.         Holder.name = Info;
    44.         }
    45.      
    46.  
    47.      
    48.  
    49.  
    50. }


    Enjoyyy :D
     
  3. Rangoric

    Rangoric

    Joined:
    Aug 17, 2014
    Posts:
    12
    Thank you. The menu item attribute was the key that I needed to figure it out :)
     
  4. jabez

    jabez

    Joined:
    Nov 2, 2013
    Posts:
    26
    No worriesss sometimes all you need is a example if you need more help let me know, take it easyyyyy :3.