2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Unity2020.1じゃなくても(0,0,0)の位置にCreate Emptyしたい!

Last updated at Posted at 2020-07-24

Create Emptyって変な位置にGame Objectを生成するよね・・・

GameObject > Create Empty してみます。
image.png
生成されたGameObjectの Position を見てみると・・・
image.png
あああああ!(0.155,0.255,0.34)ってどこ!?変な位置に生成しないで!

ってなることがよくあります。

これは、 Create Empty をする時の Scene タブの表示領域が影響しているみたいです。

どうやらUnity2020.1だと原点にオブジェクトを生成できるらしいぞ!

昨日、こちらのツイートではじめて知ったのですが、Unity2020.1には 原点でオブジェクトを生成 という設定があるようです。これを使えば変な位置に生成されなくて便利ですね!

でも、Unity2018やUnity2019を使いたいケースもまだまだあると思います。

Unity2020.1じゃなくても、オブジェクトを原点 (0,0,0) に生成する方法がないか調べてみました!

Unity2020.1じゃなくてもできる方法見つけた

調べてみたら、まったく同じ悩みの質問を見つけました。

Is there a way to force new objects to always be created at (0,0,0,)?

こちらでは拡張エディターを作る手法がベストアンサーに選ばれてました。サンプルコードが記載されてますので、そちらを参考に拡張エディターを作ってみました。

常に (0,0,0) の位置にCreate Emptyする拡張エディターを作る

Assetsフォルダー配下に Editor フォルダーを作成し、そこに GameObjectCreation.cs を作成します。内容は以下のようにします。

using UnityEngine;
using UnityEditor;

public class GameObjectCreation : MonoBehaviour
{
    [MenuItem("GameObject/Create At 0 #&0")]
    static void createAtZero()
    {
        GameObject go = new GameObject("GameObject");

        go.transform.position = Vector3.zero;
        go.transform.rotation = Quaternion.identity;
    }
}

これで拡張エディターは完成です!
(ベストアンサーのコードはもっと長いですが、とりあえず必要なところだけ抽出しました。)

メニューに以下のように Create At 0 というのが追加されています。
image.png
試しに押してみると、
image.png
ちゃんと (0,0,0) に生成されてますね!

Shift+Alt+0 でも生成できました。

さいごに

無事に解決してよかったです。本記事の作成にあたり、以下を参考にさせていただきました。ありがとうございました。

2
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?