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
1
Question by doggy_doggy · Nov 15, 2020 at 03:48 PM · scripting problemeditor-scriptinggameobjectsexecuteineditmode

How to generate objects from a script in the editor, i.e. before runtime?

I'm making a "rope" object, which is comprised of a series of links and hinges. The formation is like this:

Link Hinge Link

Links are just capsules with a Rigidbody, and Hinges are capsules that have two Hinge Joints, one linked to the link above it, one connected to the link below.

The result is a physics-based rope (more of a chain, to be honest) that swings at each Hinge as you'd expect.

Here's a screenshot of the Rope to help visualize:alt text

Obviously it's tedious to manually add Links and Hinges to increase the length of the rope. So, what I want to be able to do is edit the parameters of this rope in the inspector and have its script add Links and Hinges automatically.

My understanding is that I can run some script in editor to achieve this.

I currently have a script called "Rope" on an empty GameObject, with the expectation of generating the Rope Components (Links and Hinges) as children.

Here's the Rope script as it stands...

 [ExecuteInEditMode]
 public class Rope : MonoBehaviour
 {
     GameObject[] components = Resources.LoadAll<GameObject>("RopeComponents");
 
     [SerializeField] int ropeLength;
     [SerializeField] int numberOfLinks;
 
     public void CreateNewSection(Vector3 pos)
     {
         var newHinge = Instantiate(components[0],gameObject.transform);
         var newLink = Instantiate(components[1], gameObject.transform);
 
         MoveComps(newHinge, newLink, pos);
 
         var hinges = newHinge.GetComponents<HingeJoint>();
         hinges[1].connectedBody = newLink.GetComponent<Rigidbody>();
     }
 
     void MoveComps(GameObject hinge, GameObject link, Vector3 pos)
     {
         hinge.transform.position = pos;
         link.transform.position = pos + Vector3.down;
     }
 }



I also have an EditorScript to make a button so that I can run the CreateNewSection method from the Rope script: (Warning: it's totally in shambles, as this is the part of the process I really don't understand...)

 using UnityEditor;
 
 [CustomEditor(typeof(Rope))]
 public class RopeEditor : Editor
 {
     SerializedObject ropeObj;
 
     public override void OnInspectorGUI()
     {
         rope = serializedObject;
         DrawDefaultInspector();
         DrawRopeEditor();
     }
 
     void DrawRopeEditor()
     {
         GUILayout.Space(5);
         GUILayout.Label("Rope Editor", EditorStyles.boldLabel);
 
         DrawCommitButton();
     }
 
     void DrawCommitButton()
     {
         if (GUILayout.Button(text: "Commit"))
         {
             rope.CreateNewSection(Vector3.zero);
         }
 
     }
 }


Anyone have experience writing Editor Scripts and procedurally building objects like this?

The problem with the current way I'm attempting this is that I don't know how to reference my Rope script from my editor script so that I can call the CreateNewSection() method.

Thanks for reading, and let me know if you have any thoughts!

screen-shot-2020-11-15-at-43310-pm.png (222.4 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Zubzuke · Nov 15, 2020 at 03:57 PM

You can initialize your rope in Start() or Awake() where you can read the rope length and number of links which have been adjusted from the inspector. I don't think you need to mess with the editor itself.

Try this tutorial:

https://www.youtube.com/watch?v=FcnvwtyxLds

But if you really need to deal with the editor, try this:

https://www.youtube.com/watch?v=34736DHWzaI

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

261 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 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 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

ExecuteInEditMode not working in some scripts 1 Answer

How to create gameObjects in editor mode 1 Answer

Bullets follow the ship in 2D Space Shooter game 2 Answers

A way to know if unity was just opened? [ExecuteInEditMode] 0 Answers

How can i add a second camera that will be showing only specific gameobject in game window ? 1 Answer


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