Skip to content

Instantly share code, notes, and snippets.

@jiangdi0924
Forked from fadookie/CameraAnchor.cs
Created August 3, 2022 02:33
Show Gist options
  • Save jiangdi0924/902ae7c7f337d92b5a754f44f2eef0f7 to your computer and use it in GitHub Desktop.
Save jiangdi0924/902ae7c7f337d92b5a754f44f2eef0f7 to your computer and use it in GitHub Desktop.

Revisions

  1. @fadookie fadookie revised this gist Feb 4, 2021. 2 changed files with 198 additions and 13 deletions.
    27 changes: 14 additions & 13 deletions CameraAnchor.cs
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,7 @@
    /***
    * This script will anchor a GameObject to a relative screen position.
    * This script is intended to be used with CameraFit.cs by Marcel Căşvan, available here: http://gamedev.stackexchange.com/a/89973/50623
    * This script is intended to be used with ViewportHandler.cs by Marcel Căşvan, available here: http://gamedev.stackexchange.com/a/89973/50623
    * It is also copied in this gist below.
    *
    * Note: For performance reasons it's currently assumed that the game resolution will not change after the game starts.
    * You could not make this assumption by periodically calling UpdateAnchor() in the Update() function or a coroutine, but is left as an exercise to the reader.
    @@ -54,16 +55,16 @@ void Start () {
    }

    /// <summary>
    /// Coroutine to update the anchor only once CameraFit.Instance is not null.
    /// Coroutine to update the anchor only once ViewportHandler.Instance is not null.
    /// </summary>
    IEnumerator UpdateAnchorAsync() {
    uint cameraWaitCycles = 0;
    while(CameraFit.Instance == null) {
    while(ViewportHandler.Instance == null) {
    ++cameraWaitCycles;
    yield return new WaitForEndOfFrame();
    }
    if (cameraWaitCycles > 0) {
    print(string.Format("CameraAnchor found CameraFit instance after waiting {0} frame(s). You might want to check that CameraFit has an earlie execution order.", cameraWaitCycles));
    print(string.Format("CameraAnchor found ViewportHandler instance after waiting {0} frame(s). You might want to check that ViewportHandler has an earlie execution order.", cameraWaitCycles));
    }
    UpdateAnchor();
    updateAnchorRoutine = null;
    @@ -72,31 +73,31 @@ IEnumerator UpdateAnchorAsync() {
    void UpdateAnchor() {
    switch(anchorType) {
    case AnchorType.BottomLeft:
    SetAnchor(CameraFit.Instance.BottomLeft);
    SetAnchor(ViewportHandler.Instance.BottomLeft);
    break;
    case AnchorType.BottomCenter:
    SetAnchor(CameraFit.Instance.BottomCenter);
    SetAnchor(ViewportHandler.Instance.BottomCenter);
    break;
    case AnchorType.BottomRight:
    SetAnchor(CameraFit.Instance.BottomRight);
    SetAnchor(ViewportHandler.Instance.BottomRight);
    break;
    case AnchorType.MiddleLeft:
    SetAnchor(CameraFit.Instance.MiddleLeft);
    SetAnchor(ViewportHandler.Instance.MiddleLeft);
    break;
    case AnchorType.MiddleCenter:
    SetAnchor(CameraFit.Instance.MiddleCenter);
    SetAnchor(ViewportHandler.Instance.MiddleCenter);
    break;
    case AnchorType.MiddleRight:
    SetAnchor(CameraFit.Instance.MiddleRight);
    SetAnchor(ViewportHandler.Instance.MiddleRight);
    break;
    case AnchorType.TopLeft:
    SetAnchor(CameraFit.Instance.TopLeft);
    SetAnchor(ViewportHandler.Instance.TopLeft);
    break;
    case AnchorType.TopCenter:
    SetAnchor(CameraFit.Instance.TopCenter);
    SetAnchor(ViewportHandler.Instance.TopCenter);
    break;
    case AnchorType.TopRight:
    SetAnchor(CameraFit.Instance.TopRight);
    SetAnchor(ViewportHandler.Instance.TopRight);
    break;
    }
    }
    184 changes: 184 additions & 0 deletions ViewportHandler.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,184 @@
    // Attach this script on your main ortohgraphic camera:

    /* The MIT License (MIT)
    Copyright (c) 2014, Marcel Căşvan
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE. */

    using System;
    using System.Collections;
    using UnityEngine;

    [ExecuteInEditMode]
    [RequireComponent (typeof (Camera))]
    public class ViewportHandler : MonoBehaviour
    {
    #region FIELDS
    public Color wireColor = Color.white;
    public float UnitsSize = 1; // size of your scene in unity units
    public Constraint constraint = Constraint.Portrait;
    public static ViewportHandler Instance;
    public new Camera camera;

    private float _width;
    private float _height;
    //*** bottom screen
    private Vector3 _bl;
    private Vector3 _bc;
    private Vector3 _br;
    //*** middle screen
    private Vector3 _ml;
    private Vector3 _mc;
    private Vector3 _mr;
    //*** top screen
    private Vector3 _tl;
    private Vector3 _tc;
    private Vector3 _tr;
    #endregion

    #region PROPERTIES
    public float Width {
    get {
    return _width;
    }
    }
    public float Height {
    get {
    return _height;
    }
    }

    // helper points:
    public Vector3 BottomLeft {
    get {
    return _bl;
    }
    }
    public Vector3 BottomCenter {
    get {
    return _bc;
    }
    }
    public Vector3 BottomRight {
    get {
    return _br;
    }
    }
    public Vector3 MiddleLeft {
    get {
    return _ml;
    }
    }
    public Vector3 MiddleCenter {
    get {
    return _mc;
    }
    }
    public Vector3 MiddleRight {
    get {
    return _mr;
    }
    }
    public Vector3 TopLeft {
    get {
    return _tl;
    }
    }
    public Vector3 TopCenter {
    get {
    return _tc;
    }
    }
    public Vector3 TopRight {
    get {
    return _tr;
    }
    }
    #endregion

    #region METHODS
    private void Awake()
    {
    camera = GetComponent<Camera>();
    Instance = this;
    ComputeResolution();
    }

    private void ComputeResolution()
    {
    float leftX, rightX, topY, bottomY;

    if(constraint == Constraint.Landscape){
    camera.orthographicSize = 1f / camera.aspect * UnitsSize / 2f;
    }else{
    camera.orthographicSize = UnitsSize / 2f;
    }

    _height = 2f * camera.orthographicSize;
    _width = _height * camera.aspect;

    float cameraX, cameraY;
    cameraX = camera.transform.position.x;
    cameraY = camera.transform.position.y;

    leftX = cameraX - _width / 2;
    rightX = cameraX + _width / 2;
    topY = cameraY + _height / 2;
    bottomY = cameraY - _height / 2;

    //*** bottom
    _bl = new Vector3(leftX, bottomY, 0);
    _bc = new Vector3(cameraX, bottomY, 0);
    _br = new Vector3(rightX, bottomY, 0);
    //*** middle
    _ml = new Vector3(leftX, cameraY, 0);
    _mc = new Vector3(cameraX, cameraY, 0);
    _mr = new Vector3(rightX, cameraY, 0);
    //*** top
    _tl = new Vector3(leftX, topY, 0);
    _tc = new Vector3(cameraX, topY , 0);
    _tr = new Vector3(rightX, topY, 0);
    }

    private void Update()
    {
    #if UNITY_EDITOR
    ComputeResolution();
    #endif
    }

    void OnDrawGizmos() {
    Gizmos.color = wireColor;

    Matrix4x4 temp = Gizmos.matrix;
    Gizmos.matrix = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
    if (camera.orthographic) {
    float spread = camera.farClipPlane - camera.nearClipPlane;
    float center = (camera.farClipPlane + camera.nearClipPlane)*0.5f;
    Gizmos.DrawWireCube(new Vector3(0,0,center), new Vector3(camera.orthographicSize*2*camera.aspect, camera.orthographicSize*2, spread));
    } else {
    Gizmos.DrawFrustum(Vector3.zero, camera.fieldOfView, camera.farClipPlane, camera.nearClipPlane, camera.aspect);
    }
    Gizmos.matrix = temp;
    }
    #endregion

    public enum Constraint { Landscape, Portrait }
    }
  2. @fadookie fadookie revised this gist Feb 12, 2016. No changes.
  3. @fadookie fadookie revised this gist Jan 27, 2015. 1 changed file with 23 additions and 1 deletion.
    24 changes: 23 additions & 1 deletion CameraAnchor.cs
    Original file line number Diff line number Diff line change
    @@ -45,9 +45,28 @@ public enum AnchorType {
    public AnchorType anchorType;
    public Vector3 anchorOffset;

    IEnumerator updateAnchorRoutine; //Coroutine handle so we don't start it if it's already running

    // Use this for initialization
    void Start () {
    updateAnchorRoutine = UpdateAnchorAsync();
    StartCoroutine(updateAnchorRoutine);
    }

    /// <summary>
    /// Coroutine to update the anchor only once CameraFit.Instance is not null.
    /// </summary>
    IEnumerator UpdateAnchorAsync() {
    uint cameraWaitCycles = 0;
    while(CameraFit.Instance == null) {
    ++cameraWaitCycles;
    yield return new WaitForEndOfFrame();
    }
    if (cameraWaitCycles > 0) {
    print(string.Format("CameraAnchor found CameraFit instance after waiting {0} frame(s). You might want to check that CameraFit has an earlie execution order.", cameraWaitCycles));
    }
    UpdateAnchor();
    updateAnchorRoutine = null;
    }

    void UpdateAnchor() {
    @@ -92,7 +111,10 @@ void SetAnchor(Vector3 anchor) {
    #if UNITY_EDITOR
    // Update is called once per frame
    void Update () {
    UpdateAnchor();
    if (updateAnchorRoutine == null) {
    updateAnchorRoutine = UpdateAnchorAsync();
    StartCoroutine(updateAnchorRoutine);
    }
    }
    #endif
    }
  4. @fadookie fadookie revised this gist Jan 7, 2015. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions CameraAnchor.cs
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@
    */
    /* The MIT License (MIT)
    Copyright (c) 2014, Eliot Lash
    Copyright (c) 2015, Eliot Lash
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    @@ -83,7 +83,10 @@ void UpdateAnchor() {
    }

    void SetAnchor(Vector3 anchor) {
    transform.position = anchor + anchorOffset;
    Vector3 newPos = anchor + anchorOffset;
    if (!transform.position.Equals(newPos)) {
    transform.position = newPos;
    }
    }

    #if UNITY_EDITOR
  5. @fadookie fadookie revised this gist Jan 7, 2015. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions CameraAnchor.cs
    Original file line number Diff line number Diff line change
    @@ -31,7 +31,7 @@ THE SOFTWARE. */

    [ExecuteInEditMode]
    public class CameraAnchor : MonoBehaviour {
    public enum AnchorPoint {
    public enum AnchorType {
    BottomLeft,
    BottomCenter,
    BottomRight,
    @@ -42,7 +42,7 @@ public enum AnchorPoint {
    TopCenter,
    TopRight,
    };
    public AnchorPoint anchorType;
    public AnchorType anchorType;
    public Vector3 anchorOffset;

    // Use this for initialization
    @@ -52,31 +52,31 @@ void Start () {

    void UpdateAnchor() {
    switch(anchorType) {
    case AnchorPoint.BottomLeft:
    case AnchorType.BottomLeft:
    SetAnchor(CameraFit.Instance.BottomLeft);
    break;
    case AnchorPoint.BottomCenter:
    case AnchorType.BottomCenter:
    SetAnchor(CameraFit.Instance.BottomCenter);
    break;
    case AnchorPoint.BottomRight:
    case AnchorType.BottomRight:
    SetAnchor(CameraFit.Instance.BottomRight);
    break;
    case AnchorPoint.MiddleLeft:
    case AnchorType.MiddleLeft:
    SetAnchor(CameraFit.Instance.MiddleLeft);
    break;
    case AnchorPoint.MiddleCenter:
    case AnchorType.MiddleCenter:
    SetAnchor(CameraFit.Instance.MiddleCenter);
    break;
    case AnchorPoint.MiddleRight:
    case AnchorType.MiddleRight:
    SetAnchor(CameraFit.Instance.MiddleRight);
    break;
    case AnchorPoint.TopLeft:
    case AnchorType.TopLeft:
    SetAnchor(CameraFit.Instance.TopLeft);
    break;
    case AnchorPoint.TopCenter:
    case AnchorType.TopCenter:
    SetAnchor(CameraFit.Instance.TopCenter);
    break;
    case AnchorPoint.TopRight:
    case AnchorType.TopRight:
    SetAnchor(CameraFit.Instance.TopRight);
    break;
    }
  6. @fadookie fadookie revised this gist Jan 7, 2015. 1 changed file with 17 additions and 15 deletions.
    32 changes: 17 additions & 15 deletions CameraAnchor.cs
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,9 @@
    /***
    * This script will anchor a GameObject to a.
    * For performance reasons it's assumed that the game resolution will not change after the game starts.
    * This script will anchor a GameObject to a relative screen position.
    * This script is intended to be used with CameraFit.cs by Marcel Căşvan, available here: http://gamedev.stackexchange.com/a/89973/50623
    *
    * Note: For performance reasons it's currently assumed that the game resolution will not change after the game starts.
    * You could not make this assumption by periodically calling UpdateAnchor() in the Update() function or a coroutine, but is left as an exercise to the reader.
    */
    /* The MIT License (MIT)
    @@ -45,49 +47,49 @@ public enum AnchorPoint {

    // Use this for initialization
    void Start () {
    updateAnchor();
    UpdateAnchor();
    }

    void updateAnchor() {
    void UpdateAnchor() {
    switch(anchorType) {
    case AnchorPoint.BottomLeft:
    setAnchor(CameraFit.Instance.BottomLeft);
    SetAnchor(CameraFit.Instance.BottomLeft);
    break;
    case AnchorPoint.BottomCenter:
    setAnchor(CameraFit.Instance.BottomCenter);
    SetAnchor(CameraFit.Instance.BottomCenter);
    break;
    case AnchorPoint.BottomRight:
    setAnchor(CameraFit.Instance.BottomRight);
    SetAnchor(CameraFit.Instance.BottomRight);
    break;
    case AnchorPoint.MiddleLeft:
    setAnchor(CameraFit.Instance.MiddleLeft);
    SetAnchor(CameraFit.Instance.MiddleLeft);
    break;
    case AnchorPoint.MiddleCenter:
    setAnchor(CameraFit.Instance.MiddleCenter);
    SetAnchor(CameraFit.Instance.MiddleCenter);
    break;
    case AnchorPoint.MiddleRight:
    setAnchor(CameraFit.Instance.MiddleRight);
    SetAnchor(CameraFit.Instance.MiddleRight);
    break;
    case AnchorPoint.TopLeft:
    setAnchor(CameraFit.Instance.TopLeft);
    SetAnchor(CameraFit.Instance.TopLeft);
    break;
    case AnchorPoint.TopCenter:
    setAnchor(CameraFit.Instance.TopCenter);
    SetAnchor(CameraFit.Instance.TopCenter);
    break;
    case AnchorPoint.TopRight:
    setAnchor(CameraFit.Instance.TopRight);
    SetAnchor(CameraFit.Instance.TopRight);
    break;
    }
    }

    void setAnchor(Vector3 anchor) {
    void SetAnchor(Vector3 anchor) {
    transform.position = anchor + anchorOffset;
    }

    #if UNITY_EDITOR
    // Update is called once per frame
    void Update () {
    updateAnchor();
    UpdateAnchor();
    }
    #endif
    }
  7. @fadookie fadookie revised this gist Jan 7, 2015. 1 changed file with 93 additions and 0 deletions.
    93 changes: 93 additions & 0 deletions CameraAnchor.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    /***
    * This script will anchor a GameObject to a.
    * For performance reasons it's assumed that the game resolution will not change after the game starts.
    * This script is intended to be used with CameraFit.cs by Marcel Căşvan, available here: http://gamedev.stackexchange.com/a/89973/50623
    */
    /* The MIT License (MIT)
    Copyright (c) 2014, Eliot Lash
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE. */
    using UnityEngine;
    using System.Collections;

    [ExecuteInEditMode]
    public class CameraAnchor : MonoBehaviour {
    public enum AnchorPoint {
    BottomLeft,
    BottomCenter,
    BottomRight,
    MiddleLeft,
    MiddleCenter,
    MiddleRight,
    TopLeft,
    TopCenter,
    TopRight,
    };
    public AnchorPoint anchorType;
    public Vector3 anchorOffset;

    // Use this for initialization
    void Start () {
    updateAnchor();
    }

    void updateAnchor() {
    switch(anchorType) {
    case AnchorPoint.BottomLeft:
    setAnchor(CameraFit.Instance.BottomLeft);
    break;
    case AnchorPoint.BottomCenter:
    setAnchor(CameraFit.Instance.BottomCenter);
    break;
    case AnchorPoint.BottomRight:
    setAnchor(CameraFit.Instance.BottomRight);
    break;
    case AnchorPoint.MiddleLeft:
    setAnchor(CameraFit.Instance.MiddleLeft);
    break;
    case AnchorPoint.MiddleCenter:
    setAnchor(CameraFit.Instance.MiddleCenter);
    break;
    case AnchorPoint.MiddleRight:
    setAnchor(CameraFit.Instance.MiddleRight);
    break;
    case AnchorPoint.TopLeft:
    setAnchor(CameraFit.Instance.TopLeft);
    break;
    case AnchorPoint.TopCenter:
    setAnchor(CameraFit.Instance.TopCenter);
    break;
    case AnchorPoint.TopRight:
    setAnchor(CameraFit.Instance.TopRight);
    break;
    }
    }

    void setAnchor(Vector3 anchor) {
    transform.position = anchor + anchorOffset;
    }

    #if UNITY_EDITOR
    // Update is called once per frame
    void Update () {
    updateAnchor();
    }
    #endif
    }
  8. @fadookie fadookie created this gist Jan 7, 2015.
    21 changes: 21 additions & 0 deletions LICENSE
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    The MIT License (MIT)

    Copyright (c) 2014, Eliot Lash

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:

    The above copyright notice and this permission notice shall be included in
    all copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    THE SOFTWARE.