1.Commit
This commit is contained in:
9
Assets/Unity UI Samples/Scripts/ActiveStateToggler.cs
Normal file
9
Assets/Unity UI Samples/Scripts/ActiveStateToggler.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class ActiveStateToggler : MonoBehaviour {
|
||||
|
||||
public void ToggleActive () {
|
||||
gameObject.SetActive (!gameObject.activeSelf);
|
||||
}
|
||||
}
|
||||
15
Assets/Unity UI Samples/Scripts/ApplicationManager.cs
Normal file
15
Assets/Unity UI Samples/Scripts/ApplicationManager.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
public class ApplicationManager : MonoBehaviour {
|
||||
|
||||
|
||||
public void Quit ()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.EditorApplication.isPlaying = false;
|
||||
#else
|
||||
Application.Quit();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
50
Assets/Unity UI Samples/Scripts/ChangeColor.cs
Normal file
50
Assets/Unity UI Samples/Scripts/ChangeColor.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ChangeColor : MonoBehaviour, IPointerClickHandler
|
||||
{
|
||||
void OnEnable ()
|
||||
{
|
||||
}
|
||||
|
||||
public void SetRed(float value)
|
||||
{
|
||||
OnValueChanged(value, 0);
|
||||
}
|
||||
|
||||
public void SetGreen(float value)
|
||||
{
|
||||
OnValueChanged(value, 1);
|
||||
}
|
||||
|
||||
public void SetBlue(float value)
|
||||
{
|
||||
OnValueChanged(value, 2);
|
||||
}
|
||||
|
||||
public void OnValueChanged(float value, int channel)
|
||||
{
|
||||
Color c = Color.white;
|
||||
|
||||
if (GetComponent<Renderer>() != null)
|
||||
c = GetComponent<Renderer>().material.color;
|
||||
else if (GetComponent<Light>() != null)
|
||||
c = GetComponent<Light>().color;
|
||||
|
||||
c[channel] = value;
|
||||
|
||||
if (GetComponent<Renderer>() != null)
|
||||
GetComponent<Renderer>().material.color = c;
|
||||
else if (GetComponent<Light>() != null)
|
||||
GetComponent<Light>().color = c;
|
||||
}
|
||||
|
||||
public void OnPointerClick(PointerEventData data)
|
||||
{
|
||||
if (GetComponent<Renderer>() != null)
|
||||
GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value, 1.0f);
|
||||
else if (GetComponent<Light>() != null)
|
||||
GetComponent<Light>().color = new Color(Random.value, Random.value, Random.value, 1.0f);
|
||||
}
|
||||
}
|
||||
88
Assets/Unity UI Samples/Scripts/DragMe.cs
Normal file
88
Assets/Unity UI Samples/Scripts/DragMe.cs
Normal file
@@ -0,0 +1,88 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(Image))]
|
||||
public class DragMe : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
|
||||
{
|
||||
public bool dragOnSurfaces = true;
|
||||
|
||||
private Dictionary<int,GameObject> m_DraggingIcons = new Dictionary<int, GameObject>();
|
||||
private Dictionary<int, RectTransform> m_DraggingPlanes = new Dictionary<int, RectTransform>();
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
var canvas = FindInParents<Canvas>(gameObject);
|
||||
if (canvas == null)
|
||||
return;
|
||||
|
||||
// We have clicked something that can be dragged.
|
||||
// What we want to do is create an icon for this.
|
||||
m_DraggingIcons[eventData.pointerId] = new GameObject("icon");
|
||||
|
||||
m_DraggingIcons[eventData.pointerId].transform.SetParent (canvas.transform, false);
|
||||
m_DraggingIcons[eventData.pointerId].transform.SetAsLastSibling();
|
||||
|
||||
var image = m_DraggingIcons[eventData.pointerId].AddComponent<Image>();
|
||||
// The icon will be under the cursor.
|
||||
// We want it to be ignored by the event system.
|
||||
var group = m_DraggingIcons[eventData.pointerId].AddComponent<CanvasGroup>();
|
||||
group.blocksRaycasts = false;
|
||||
|
||||
image.sprite = GetComponent<Image>().sprite;
|
||||
image.SetNativeSize();
|
||||
|
||||
if (dragOnSurfaces)
|
||||
m_DraggingPlanes[eventData.pointerId] = transform as RectTransform;
|
||||
else
|
||||
m_DraggingPlanes[eventData.pointerId] = canvas.transform as RectTransform;
|
||||
|
||||
SetDraggedPosition(eventData);
|
||||
}
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
if (m_DraggingIcons[eventData.pointerId] != null)
|
||||
SetDraggedPosition(eventData);
|
||||
}
|
||||
|
||||
private void SetDraggedPosition(PointerEventData eventData)
|
||||
{
|
||||
if (dragOnSurfaces && eventData.pointerEnter != null && eventData.pointerEnter.transform as RectTransform != null)
|
||||
m_DraggingPlanes[eventData.pointerId] = eventData.pointerEnter.transform as RectTransform;
|
||||
|
||||
var rt = m_DraggingIcons[eventData.pointerId].GetComponent<RectTransform>();
|
||||
Vector3 globalMousePos;
|
||||
if (RectTransformUtility.ScreenPointToWorldPointInRectangle(m_DraggingPlanes[eventData.pointerId], eventData.position, eventData.pressEventCamera, out globalMousePos))
|
||||
{
|
||||
rt.position = globalMousePos;
|
||||
rt.rotation = m_DraggingPlanes[eventData.pointerId].rotation;
|
||||
}
|
||||
}
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
if (m_DraggingIcons[eventData.pointerId] != null)
|
||||
Destroy(m_DraggingIcons[eventData.pointerId]);
|
||||
|
||||
m_DraggingIcons[eventData.pointerId] = null;
|
||||
}
|
||||
|
||||
static public T FindInParents<T>(GameObject go) where T : Component
|
||||
{
|
||||
if (go == null) return null;
|
||||
var comp = go.GetComponent<T>();
|
||||
|
||||
if (comp != null)
|
||||
return comp;
|
||||
|
||||
var t = go.transform.parent;
|
||||
while (t != null && comp == null)
|
||||
{
|
||||
comp = t.gameObject.GetComponent<T>();
|
||||
t = t.parent;
|
||||
}
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
48
Assets/Unity UI Samples/Scripts/DragPanel.cs
Normal file
48
Assets/Unity UI Samples/Scripts/DragPanel.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
using System.Collections;
|
||||
|
||||
public class DragPanel : MonoBehaviour, IPointerDownHandler, IDragHandler {
|
||||
|
||||
private Vector2 originalLocalPointerPosition;
|
||||
private Vector3 originalPanelLocalPosition;
|
||||
private RectTransform panelRectTransform;
|
||||
private RectTransform parentRectTransform;
|
||||
|
||||
void Awake () {
|
||||
panelRectTransform = transform.parent as RectTransform;
|
||||
parentRectTransform = panelRectTransform.parent as RectTransform;
|
||||
}
|
||||
|
||||
public void OnPointerDown (PointerEventData data) {
|
||||
originalPanelLocalPosition = panelRectTransform.localPosition;
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);
|
||||
}
|
||||
|
||||
public void OnDrag (PointerEventData data) {
|
||||
if (panelRectTransform == null || parentRectTransform == null)
|
||||
return;
|
||||
|
||||
Vector2 localPointerPosition;
|
||||
if (RectTransformUtility.ScreenPointToLocalPointInRectangle (parentRectTransform, data.position, data.pressEventCamera, out localPointerPosition)) {
|
||||
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
|
||||
panelRectTransform.localPosition = originalPanelLocalPosition + offsetToOriginal;
|
||||
}
|
||||
|
||||
ClampToWindow ();
|
||||
}
|
||||
|
||||
// Clamp panel to area of parent
|
||||
void ClampToWindow () {
|
||||
Vector3 pos = panelRectTransform.localPosition;
|
||||
|
||||
Vector3 minPosition = parentRectTransform.rect.min - panelRectTransform.rect.min;
|
||||
Vector3 maxPosition = parentRectTransform.rect.max - panelRectTransform.rect.max;
|
||||
|
||||
pos.x = Mathf.Clamp (panelRectTransform.localPosition.x, minPosition.x, maxPosition.x);
|
||||
pos.y = Mathf.Clamp (panelRectTransform.localPosition.y, minPosition.y, maxPosition.y);
|
||||
|
||||
panelRectTransform.localPosition = pos;
|
||||
}
|
||||
}
|
||||
65
Assets/Unity UI Samples/Scripts/DropMe.cs
Normal file
65
Assets/Unity UI Samples/Scripts/DropMe.cs
Normal file
@@ -0,0 +1,65 @@
|
||||
using System.Reflection;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class DropMe : MonoBehaviour, IDropHandler, IPointerEnterHandler, IPointerExitHandler
|
||||
{
|
||||
public Image containerImage;
|
||||
public Image receivingImage;
|
||||
private Color normalColor;
|
||||
public Color highlightColor = Color.yellow;
|
||||
|
||||
public void OnEnable ()
|
||||
{
|
||||
if (containerImage != null)
|
||||
normalColor = containerImage.color;
|
||||
}
|
||||
|
||||
public void OnDrop(PointerEventData data)
|
||||
{
|
||||
containerImage.color = normalColor;
|
||||
|
||||
if (receivingImage == null)
|
||||
return;
|
||||
|
||||
Sprite dropSprite = GetDropSprite (data);
|
||||
if (dropSprite != null)
|
||||
receivingImage.overrideSprite = dropSprite;
|
||||
}
|
||||
|
||||
public void OnPointerEnter(PointerEventData data)
|
||||
{
|
||||
if (containerImage == null)
|
||||
return;
|
||||
|
||||
Sprite dropSprite = GetDropSprite (data);
|
||||
if (dropSprite != null)
|
||||
containerImage.color = highlightColor;
|
||||
}
|
||||
|
||||
public void OnPointerExit(PointerEventData data)
|
||||
{
|
||||
if (containerImage == null)
|
||||
return;
|
||||
|
||||
containerImage.color = normalColor;
|
||||
}
|
||||
|
||||
private Sprite GetDropSprite(PointerEventData data)
|
||||
{
|
||||
var originalObj = data.pointerDrag;
|
||||
if (originalObj == null)
|
||||
return null;
|
||||
|
||||
var dragMe = originalObj.GetComponent<DragMe>();
|
||||
if (dragMe == null)
|
||||
return null;
|
||||
|
||||
var srcImage = originalObj.GetComponent<Image>();
|
||||
if (srcImage == null)
|
||||
return null;
|
||||
|
||||
return srcImage.sprite;
|
||||
}
|
||||
}
|
||||
96
Assets/Unity UI Samples/Scripts/PanelManager.cs
Normal file
96
Assets/Unity UI Samples/Scripts/PanelManager.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class PanelManager : MonoBehaviour {
|
||||
|
||||
public Animator initiallyOpen;
|
||||
|
||||
private int m_OpenParameterId;
|
||||
private Animator m_Open;
|
||||
private GameObject m_PreviouslySelected;
|
||||
|
||||
const string k_OpenTransitionName = "Open";
|
||||
const string k_ClosedStateName = "Closed";
|
||||
|
||||
public void OnEnable()
|
||||
{
|
||||
m_OpenParameterId = Animator.StringToHash (k_OpenTransitionName);
|
||||
|
||||
if (initiallyOpen == null)
|
||||
return;
|
||||
|
||||
OpenPanel(initiallyOpen);
|
||||
}
|
||||
|
||||
public void OpenPanel (Animator anim)
|
||||
{
|
||||
if (m_Open == anim)
|
||||
return;
|
||||
|
||||
anim.gameObject.SetActive(true);
|
||||
var newPreviouslySelected = EventSystem.current.currentSelectedGameObject;
|
||||
|
||||
anim.transform.SetAsLastSibling();
|
||||
|
||||
CloseCurrent();
|
||||
|
||||
m_PreviouslySelected = newPreviouslySelected;
|
||||
|
||||
m_Open = anim;
|
||||
m_Open.SetBool(m_OpenParameterId, true);
|
||||
|
||||
GameObject go = FindFirstEnabledSelectable(anim.gameObject);
|
||||
|
||||
SetSelected(go);
|
||||
}
|
||||
|
||||
static GameObject FindFirstEnabledSelectable (GameObject gameObject)
|
||||
{
|
||||
GameObject go = null;
|
||||
var selectables = gameObject.GetComponentsInChildren<Selectable> (true);
|
||||
foreach (var selectable in selectables) {
|
||||
if (selectable.IsActive () && selectable.IsInteractable ()) {
|
||||
go = selectable.gameObject;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return go;
|
||||
}
|
||||
|
||||
public void CloseCurrent()
|
||||
{
|
||||
if (m_Open == null)
|
||||
return;
|
||||
|
||||
m_Open.SetBool(m_OpenParameterId, false);
|
||||
SetSelected(m_PreviouslySelected);
|
||||
StartCoroutine(DisablePanelDeleyed(m_Open));
|
||||
m_Open = null;
|
||||
}
|
||||
|
||||
IEnumerator DisablePanelDeleyed(Animator anim)
|
||||
{
|
||||
bool closedStateReached = false;
|
||||
bool wantToClose = true;
|
||||
while (!closedStateReached && wantToClose)
|
||||
{
|
||||
if (!anim.IsInTransition(0))
|
||||
closedStateReached = anim.GetCurrentAnimatorStateInfo(0).IsName(k_ClosedStateName);
|
||||
|
||||
wantToClose = !anim.GetBool(m_OpenParameterId);
|
||||
|
||||
yield return new WaitForEndOfFrame();
|
||||
}
|
||||
|
||||
if (wantToClose)
|
||||
anim.gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
private void SetSelected(GameObject go)
|
||||
{
|
||||
EventSystem.current.SetSelectedGameObject(go);
|
||||
}
|
||||
}
|
||||
39
Assets/Unity UI Samples/Scripts/ResizePanel.cs
Normal file
39
Assets/Unity UI Samples/Scripts/ResizePanel.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
public class ResizePanel : MonoBehaviour, IPointerDownHandler, IDragHandler {
|
||||
|
||||
public Vector2 minSize = new Vector2 (100, 100);
|
||||
public Vector2 maxSize = new Vector2 (400, 400);
|
||||
|
||||
private RectTransform panelRectTransform;
|
||||
private Vector2 originalLocalPointerPosition;
|
||||
private Vector2 originalSizeDelta;
|
||||
|
||||
void Awake () {
|
||||
panelRectTransform = transform.parent.GetComponent<RectTransform> ();
|
||||
}
|
||||
|
||||
public void OnPointerDown (PointerEventData data) {
|
||||
originalSizeDelta = panelRectTransform.sizeDelta;
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle (panelRectTransform, data.position, data.pressEventCamera, out originalLocalPointerPosition);
|
||||
}
|
||||
|
||||
public void OnDrag (PointerEventData data) {
|
||||
if (panelRectTransform == null)
|
||||
return;
|
||||
|
||||
Vector2 localPointerPosition;
|
||||
RectTransformUtility.ScreenPointToLocalPointInRectangle (panelRectTransform, data.position, data.pressEventCamera, out localPointerPosition);
|
||||
Vector3 offsetToOriginal = localPointerPosition - originalLocalPointerPosition;
|
||||
|
||||
Vector2 sizeDelta = originalSizeDelta + new Vector2 (offsetToOriginal.x, -offsetToOriginal.y);
|
||||
sizeDelta = new Vector2 (
|
||||
Mathf.Clamp (sizeDelta.x, minSize.x, maxSize.x),
|
||||
Mathf.Clamp (sizeDelta.y, minSize.y, maxSize.y)
|
||||
);
|
||||
|
||||
panelRectTransform.sizeDelta = sizeDelta;
|
||||
}
|
||||
}
|
||||
64
Assets/Unity UI Samples/Scripts/ScrollDetailTexture.cs
Normal file
64
Assets/Unity UI Samples/Scripts/ScrollDetailTexture.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
|
||||
[RequireComponent(typeof(Image))]
|
||||
public class ScrollDetailTexture : MonoBehaviour
|
||||
{
|
||||
public bool uniqueMaterial = false;
|
||||
public Vector2 scrollPerSecond = Vector2.zero;
|
||||
|
||||
Matrix4x4 m_Matrix;
|
||||
Material mCopy;
|
||||
Material mOriginal;
|
||||
Image mSprite;
|
||||
Material m_Mat;
|
||||
|
||||
void OnEnable ()
|
||||
{
|
||||
mSprite = GetComponent<Image>();
|
||||
mOriginal = mSprite.material;
|
||||
|
||||
if (uniqueMaterial && mSprite.material != null)
|
||||
{
|
||||
mCopy = new Material(mOriginal);
|
||||
mCopy.name = "Copy of " + mOriginal.name;
|
||||
mCopy.hideFlags = HideFlags.DontSave;
|
||||
mSprite.material = mCopy;
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable ()
|
||||
{
|
||||
if (mCopy != null)
|
||||
{
|
||||
mSprite.material = mOriginal;
|
||||
if (Application.isEditor)
|
||||
UnityEngine.Object.DestroyImmediate(mCopy);
|
||||
else
|
||||
UnityEngine.Object.Destroy(mCopy);
|
||||
mCopy = null;
|
||||
}
|
||||
mOriginal = null;
|
||||
}
|
||||
|
||||
void Update ()
|
||||
{
|
||||
Material mat = (mCopy != null) ? mCopy : mOriginal;
|
||||
|
||||
if (mat != null)
|
||||
{
|
||||
Texture tex = mat.GetTexture("_DetailTex");
|
||||
|
||||
if (tex != null)
|
||||
{
|
||||
mat.SetTextureOffset("_DetailTex", scrollPerSecond * Time.time);
|
||||
|
||||
// TODO: It would be better to add support for MaterialBlocks on UIRenderer,
|
||||
// because currently only one Update() function's matrix can be active at a time.
|
||||
// With material block properties, the batching would be correctly broken up instead,
|
||||
// and would work with multiple widgets using this detail shader.
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Assets/Unity UI Samples/Scripts/ShowSliderValue.cs
Normal file
14
Assets/Unity UI Samples/Scripts/ShowSliderValue.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System.Reflection.Emit;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(Text))]
|
||||
public class ShowSliderValue : MonoBehaviour
|
||||
{
|
||||
public void UpdateLabel (float value)
|
||||
{
|
||||
Text lbl = GetComponent<Text>();
|
||||
if (lbl != null)
|
||||
lbl.text = Mathf.RoundToInt (value * 100) + "%";
|
||||
}
|
||||
}
|
||||
29
Assets/Unity UI Samples/Scripts/TiltWindow.cs
Normal file
29
Assets/Unity UI Samples/Scripts/TiltWindow.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using UnityEngine;
|
||||
|
||||
public class TiltWindow : MonoBehaviour
|
||||
{
|
||||
public Vector2 range = new Vector2(5f, 3f);
|
||||
|
||||
Transform mTrans;
|
||||
Quaternion mStart;
|
||||
Vector2 mRot = Vector2.zero;
|
||||
|
||||
void Start ()
|
||||
{
|
||||
mTrans = transform;
|
||||
mStart = mTrans.localRotation;
|
||||
}
|
||||
|
||||
void Update ()
|
||||
{
|
||||
Vector3 pos = Input.mousePosition;
|
||||
|
||||
float halfWidth = Screen.width * 0.5f;
|
||||
float halfHeight = Screen.height * 0.5f;
|
||||
float x = Mathf.Clamp((pos.x - halfWidth) / halfWidth, -1f, 1f);
|
||||
float y = Mathf.Clamp((pos.y - halfHeight) / halfHeight, -1f, 1f);
|
||||
mRot = Vector2.Lerp(mRot, new Vector2(x, y), Time.deltaTime * 5f);
|
||||
|
||||
mTrans.localRotation = mStart * Quaternion.Euler(-mRot.y * range.y, mRot.x * range.x, 0f);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user