123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- /**
- * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
- *
- * You are hereby granted a non-exclusive, worldwide, royalty-free license to use,
- * copy, modify, and distribute this software in source code or binary form for use
- * in connection with the web services and APIs provided by Facebook.
- *
- * As with any software that integrates with the Facebook platform, your use of
- * this software is subject to the Facebook Developer Principles and Policies
- * [http://developers.facebook.com/policy/]. This copyright 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.
- */
- namespace Facebook.Unity.Example
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- internal class ConsoleBase : MonoBehaviour
- {
- private const int DpiScalingFactor = 160;
- private static Stack<string> menuStack = new Stack<string>();
- private string status = "Ready";
- private string lastResponse = string.Empty;
- private Vector2 scrollPosition = Vector2.zero;
- // DPI scaling
- private float? scaleFactor;
- private GUIStyle textStyle;
- private GUIStyle buttonStyle;
- private GUIStyle textInputStyle;
- private GUIStyle labelStyle;
- protected static int ButtonHeight
- {
- get
- {
- return Constants.IsMobile ? 60 : 24;
- }
- }
- protected static int MainWindowWidth
- {
- get
- {
- return Constants.IsMobile ? Screen.width - 30 : 700;
- }
- }
- protected static int MainWindowFullWidth
- {
- get
- {
- return Constants.IsMobile ? Screen.width : 760;
- }
- }
- protected static int MarginFix
- {
- get
- {
- return Constants.IsMobile ? 0 : 48;
- }
- }
- protected static Stack<string> MenuStack
- {
- get
- {
- return ConsoleBase.menuStack;
- }
- set
- {
- ConsoleBase.menuStack = value;
- }
- }
- protected string Status
- {
- get
- {
- return this.status;
- }
- set
- {
- this.status = value;
- }
- }
- protected Texture2D LastResponseTexture { get; set; }
- protected string LastResponse
- {
- get
- {
- return this.lastResponse;
- }
- set
- {
- this.lastResponse = value;
- }
- }
- protected Vector2 ScrollPosition
- {
- get
- {
- return this.scrollPosition;
- }
- set
- {
- this.scrollPosition = value;
- }
- }
- // Note we assume that these styles will be accessed from OnGUI otherwise the
- // unity APIs will fail.
- protected float ScaleFactor
- {
- get
- {
- if (!this.scaleFactor.HasValue)
- {
- this.scaleFactor = Screen.dpi / ConsoleBase.DpiScalingFactor;
- }
- return this.scaleFactor.Value;
- }
- }
- protected int FontSize
- {
- get
- {
- return (int)Math.Round(this.ScaleFactor * 16);
- }
- }
- protected GUIStyle TextStyle
- {
- get
- {
- if (this.textStyle == null)
- {
- this.textStyle = new GUIStyle(GUI.skin.textArea);
- this.textStyle.alignment = TextAnchor.UpperLeft;
- this.textStyle.wordWrap = true;
- this.textStyle.padding = new RectOffset(10, 10, 10, 10);
- this.textStyle.stretchHeight = true;
- this.textStyle.stretchWidth = false;
- this.textStyle.fontSize = this.FontSize;
- }
- return this.textStyle;
- }
- }
- protected GUIStyle ButtonStyle
- {
- get
- {
- if (this.buttonStyle == null)
- {
- this.buttonStyle = new GUIStyle(GUI.skin.button);
- this.buttonStyle.fontSize = this.FontSize;
- }
- return this.buttonStyle;
- }
- }
- protected GUIStyle TextInputStyle
- {
- get
- {
- if (this.textInputStyle == null)
- {
- this.textInputStyle = new GUIStyle(GUI.skin.textField);
- this.textInputStyle.fontSize = this.FontSize;
- }
- return this.textInputStyle;
- }
- }
- protected GUIStyle LabelStyle
- {
- get
- {
- if (this.labelStyle == null)
- {
- this.labelStyle = new GUIStyle(GUI.skin.label);
- this.labelStyle.fontSize = this.FontSize;
- }
- return this.labelStyle;
- }
- }
- protected virtual void Awake()
- {
- // Limit the framerate to 60 to keep device from burning through cpu
- Application.targetFrameRate = 60;
- }
- protected bool Button(string label)
- {
- return GUILayout.Button(
- label,
- this.ButtonStyle,
- GUILayout.MinHeight(ConsoleBase.ButtonHeight * this.ScaleFactor),
- GUILayout.MaxWidth(ConsoleBase.MainWindowWidth));
- }
- protected void LabelAndTextField(string label, ref string text)
- {
- GUILayout.BeginHorizontal();
- GUILayout.Label(label, this.LabelStyle, GUILayout.MaxWidth(200 * this.ScaleFactor));
- text = GUILayout.TextField(
- text,
- this.TextInputStyle,
- GUILayout.MaxWidth(ConsoleBase.MainWindowWidth - 150));
- GUILayout.EndHorizontal();
- }
- protected bool IsHorizontalLayout()
- {
- #if UNITY_IOS || UNITY_ANDROID
- return Screen.orientation == ScreenOrientation.Landscape;
- #else
- return true;
- #endif
- }
- protected void SwitchMenu(Type menuClass)
- {
- ConsoleBase.menuStack.Push(this.GetType().Name);
- SceneManager.LoadScene(menuClass.Name);
- }
- protected void GoBack()
- {
- if (ConsoleBase.menuStack.Any())
- {
- SceneManager.LoadScene(ConsoleBase.menuStack.Pop());
- }
- }
- }
- }
|