CreateTorus

From Unify Community Wiki
Revision as of 04:58, 4 April 2007 by Aarku (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Author: Jonathan Czeck (aarku)

Description

This editor script creates a pseudo-torus at the origin with parameters radius and thickness.

Usage

Place this script in YourProject/Assets/Editor and a menu item will automatically appear in the "GameObject/Create Other" menu after it is compiled.

C# - CreateTorus.cs

<csharp>using UnityEngine; using UnityEditor; using System.Collections;

public class CreateTorus : ScriptableWizard {

   public float radius = 1f;
   public float thickness = 0.5f;
   
   [MenuItem ("GameObject/Create Other/Torus")]
   static void CreateWizard()
   {
       ScriptableWizard.DisplayWizard("Create Torus", typeof(CreateTorus));
   }
   
   void OnWizardCreate()
   {
       float diameter = radius * 2f;
       float circumference = Mathf.PI * diameter;
       float interval = 360f / (circumference / (thickness * 2f));
       
       GameObject torus = new GameObject("Torus");
       
       for (float i = 0f; i < 360f; i += interval)
       {
           AddSphereCollider(torus.transform, Quaternion.Euler(0f, i, 0f) * new Vector3(radius, 0f, 0f), Quaternion.identity, thickness);
       }
       
       Selection.activeObject = torus;
   }
   
   void AddSphereCollider(Transform parent, Vector3 position, Quaternion rotation, float radius)
   {
       GameObject go = new GameObject("Sphere Collider");
       SphereCollider sphereCollider = go.AddComponent(typeof(SphereCollider)) as SphereCollider;
       sphereCollider.radius = radius;
       go.transform.position = position;
       go.transform.rotation = rotation;
       go.transform.parent = parent;
   }

} </csharp>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox