From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Text replace - "<csharp>" to "<syntaxhighlight lang="csharp">")
m (Text replace - "</csharp>" to "</syntaxhighlight>")
Line 58: Line 58:

Latest revision as of 20:45, 10 January 2012

Author: Jonathan Czeck (aarku)


[edit] Description

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

[edit] 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.

[edit] Problems

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.

[edit] 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