Skip to content

Instantly share code, notes, and snippets.

@tsubaki
Created March 17, 2014 16:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsubaki/9603045 to your computer and use it in GitHub Desktop.
Save tsubaki/9603045 to your computer and use it in GitHub Desktop.
maincameraから見た左上・右下等にゲームオブジェクトを移動する。左上を(0,0)にしたい場合等
using UnityEngine;
using System.Collections;
public class EasyAnchor : MonoBehaviour
{
public enum VerticalPosition
{
Top = 2,
Center = 1,
Bottom = 0
}
public enum HorizontalPosition
{
Left = 0,
Center = 1,
Right = 2
}
public VerticalPosition vertical;
public HorizontalPosition horizonal;
[ContextMenu("Anchor")]
void MoveAnchor ()
{
float v = (float)vertical / 2;
float h = (float)horizonal / 2;
Vector3 pos = Camera.main.ViewportToWorldPoint (new Vector3 (h, v, -Camera.main.transform.position.z));
transform.position = pos;
}
void OnValidate ()
{
MoveAnchor ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment