From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Reverted edits by Joseph05408 (Talk); changed back to last version by Aarku)
m (Text replace - "<csharp>" to "<syntaxhighlight lang="csharp">")
Line 17: Line 17:
==C# - CreateTorus.cs==
==C# - CreateTorus.cs==
<csharp>using UnityEngine;
<syntaxhighlight lang="csharp">using UnityEngine;
using UnityEditor;
using UnityEditor;
using System.Collections;
using System.Collections;

Revision as of 10:26, 10 January 2012

Author: Jonathan Czeck (aarku)



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


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


These aren't true tori so they won't act totally correct. Also, there can be two overlapping spheres depending on the radius and thickness. You might want to choose values for radius or thickness to minimize this, or improve the script.

C# - CreateTorus.cs

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;
Personal tools