You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
290 lines
8.2 KiB
290 lines
8.2 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
public class CharacterBehaviour : MonoBehaviour {
|
|
|
|
[SerializeField]
|
|
private bool standby = true;
|
|
|
|
|
|
[Range(3, 15)]
|
|
public float DisAppearTime = 5;
|
|
|
|
public Material mat;
|
|
|
|
public Area area;
|
|
public Vector2 WallROI;
|
|
public float bottomHeight = 0.1f;
|
|
public float minHeight = 0;
|
|
public float MaxHeight = 0.5f;
|
|
[SerializeField]
|
|
private RectTransform m_recttrans;
|
|
|
|
public DShowClip standbyclip;
|
|
public DShowClip TriggerClip;
|
|
[SerializeField]
|
|
private DShowMoviePlayer[] players;
|
|
private MeshRenderer m_meshRender;
|
|
public Color color;
|
|
private bool isSleep;
|
|
|
|
public float VideoStopTime = 1.5f;
|
|
|
|
private void Awake()
|
|
{
|
|
m_meshRender = GetComponent<MeshRenderer>();
|
|
mat = new Material(Shader.Find("Unlit/ColorTransparent"));
|
|
m_meshRender.material = mat;
|
|
List<Material> mats = new List<Material>();
|
|
mats.Add(mat);
|
|
InitialDSPlayer();
|
|
}
|
|
|
|
void InitialDSPlayer()
|
|
{
|
|
GameObject TriggerObj = new GameObject("Trigger");
|
|
TriggerObj.transform.parent = this.transform;
|
|
SetClip(TriggerObj.AddComponent<DShowMoviePlayer>(), TriggerClip);
|
|
GameObject standbyObj = new GameObject("Standby");
|
|
standbyObj.transform.parent = this.transform;
|
|
SetClip(standbyObj.AddComponent<DShowMoviePlayer>(), standbyclip);
|
|
players = GetComponentsInChildren<DShowMoviePlayer>();
|
|
}
|
|
|
|
void SetClip(DShowMoviePlayer player,DShowClip clip)
|
|
{
|
|
player.VideoAsset = clip;
|
|
player.Load();
|
|
}
|
|
|
|
void InitialTouchButton()
|
|
{
|
|
if (m_recttrans != null)
|
|
return;
|
|
GameObject obj = new GameObject(gameObject.name);
|
|
obj.transform.parent = GameObject.Find("Final Canvas").transform;
|
|
m_recttrans = obj.AddComponent<RectTransform>();
|
|
m_recttrans.anchorMin = new Vector2(0, 0);
|
|
m_recttrans.anchorMax = new Vector2(0, 0);
|
|
m_recttrans.pivot = new Vector2(0.5f, 0.5f);
|
|
m_recttrans.sizeDelta = new Vector2(transform.localScale.x, transform.localScale.y) * FrozenScreenToWorldSpace.Instance.finalPixelsByMeter;
|
|
m_recttrans.gameObject.AddComponent<RawImage>();
|
|
m_recttrans.GetComponent<RawImage>().color = new Color(1, 1, 1, 0);
|
|
TouchArea toucharea = m_recttrans.gameObject.AddComponent<TouchArea>();
|
|
toucharea.PointerDown.AddListener((data)=> { Trigger(data); });
|
|
toucharea.PointerDrag.AddListener((data) => { Trigger(data); });
|
|
}
|
|
|
|
|
|
// Use this for initialization
|
|
void Start () {
|
|
|
|
standby = true;
|
|
isSleep = false;
|
|
mat.mainTextureScale = new Vector2(1, -1);
|
|
mat.mainTextureOffset = new Vector2(0,-1);
|
|
StartCoroutine(PlayOnStart());
|
|
InitialTouchButton();
|
|
StartCoroutine(RandomPosNoTriggerOther());
|
|
}
|
|
|
|
IEnumerator PlayOnStart()
|
|
{
|
|
DShowMoviePlayer player = currentPlayer;
|
|
player.Loop = true;
|
|
while (!player.IsPlaying)
|
|
{
|
|
player.Play();
|
|
mat.mainTexture = player.Texture;
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update () {
|
|
|
|
if (Input.GetKeyDown(KeyCode.R))
|
|
{
|
|
if(!isTriggerCollider)
|
|
StartCoroutine(RandomPosNoTriggerOther());
|
|
}
|
|
|
|
UpdateMaterial();
|
|
|
|
if (!isSleep)
|
|
{
|
|
if (!standby)
|
|
CheckPlayFinished();
|
|
}
|
|
}
|
|
|
|
void UpdateMaterial()
|
|
{
|
|
mat.mainTexture = currentPlayer.Texture;
|
|
mat.SetColor("_Color", color);
|
|
}
|
|
|
|
void CheckPlayFinished()
|
|
{
|
|
if(currentPlayer.IsFinished)
|
|
{
|
|
StartCoroutine(Sleep());
|
|
}
|
|
}
|
|
|
|
void RandomAppearWall()
|
|
{
|
|
Vector3 newPos = Vector3.zero;
|
|
area = (Area)Random.Range(0, 3);
|
|
Quaternion q = new Quaternion();
|
|
if (area == Area.LeftWall)
|
|
{
|
|
newPos.x = -FrozenScreenToWorldSpace.Instance.length / 2;
|
|
q.SetLookRotation(Vector3.left);
|
|
}
|
|
if (area == Area.RightWall)
|
|
{
|
|
newPos.x = FrozenScreenToWorldSpace.Instance.length / 2;
|
|
q.SetLookRotation(Vector3.right);
|
|
}
|
|
if (area == Area.TopWall)
|
|
{
|
|
newPos.z = FrozenScreenToWorldSpace.Instance.width / 2;
|
|
q.SetLookRotation(Vector3.forward);
|
|
}
|
|
transform.rotation = q;
|
|
transform.localPosition = newPos;
|
|
RandomPos();
|
|
}
|
|
|
|
void UpdatePos()
|
|
{
|
|
Vector3 n_pos = transform.localPosition;
|
|
if (area == Area.TopWall)
|
|
n_pos.x = Mathf.Lerp(-FrozenScreenToWorldSpace.Instance.length / 2, FrozenScreenToWorldSpace.Instance.length / 2, WallROI.x);
|
|
else
|
|
n_pos.z = Mathf.Lerp(-FrozenScreenToWorldSpace.Instance.width / 2, FrozenScreenToWorldSpace.Instance.width / 2, WallROI.x);
|
|
n_pos.y = Mathf.Lerp(0, (float)FrozenScreenToWorldSpace.Instance.height, WallROI.y);
|
|
transform.localPosition = n_pos;
|
|
SetButtonPos();
|
|
}
|
|
|
|
|
|
void RandomPos()
|
|
{
|
|
Vector3 randomPos = transform.localPosition;
|
|
float minX = transform.localScale.x / 2;
|
|
|
|
if (randomPos.x == 0)
|
|
WallROI.x = Random.Range(minX, FrozenScreenToWorldSpace.Instance.length - minX) / FrozenScreenToWorldSpace.Instance.length;
|
|
else
|
|
WallROI.x = Random.Range(minX, FrozenScreenToWorldSpace.Instance.width - minX) / FrozenScreenToWorldSpace.Instance.width;
|
|
|
|
WallROI.y = Random.Range((transform.localScale.y/2) - transform.localScale.y * bottomHeight + minHeight, MaxHeight) / (float)FrozenScreenToWorldSpace.Instance.height;
|
|
UpdatePos();
|
|
SetButtonPos();
|
|
}
|
|
|
|
void SetButtonPos()
|
|
{
|
|
Vector2 newScreenPos = FrozenScreenToWorldSpace.Instance.GetFinalScreenPos(area, WallROI);
|
|
m_recttrans.anchoredPosition = newScreenPos;
|
|
}
|
|
|
|
private void Trigger(PointerEventData data)
|
|
{
|
|
if (!standby || isSleep)
|
|
return;
|
|
standby = false;
|
|
currentPlayer.Play();
|
|
mat.mainTexture = currentPlayer.Texture;
|
|
}
|
|
|
|
void Idle()
|
|
{
|
|
standby = true;
|
|
currentPlayer.Play();
|
|
}
|
|
|
|
DShowMoviePlayer currentPlayer { get { return players[System.Convert.ToInt32(standby)]; } }
|
|
|
|
IEnumerator Sleep()
|
|
{
|
|
isSleep = true;
|
|
yield return StartCoroutine(WaitVideoStopTimeFadeOut());
|
|
yield return new WaitForSeconds(GetDisAppear());
|
|
yield return StartCoroutine(RandomPosNoTriggerOther());
|
|
yield return StartCoroutine(WaitVideoStandbyFadeIn());
|
|
isSleep = false;
|
|
Idle();
|
|
}
|
|
|
|
IEnumerator Fade(float targetA)
|
|
{
|
|
float value = 0;
|
|
float origin = color.a;
|
|
while(value < 1)
|
|
{
|
|
value = Mathf.Clamp(value + Time.deltaTime, 0,1);
|
|
color.a = Mathf.Lerp(origin, targetA, value);
|
|
yield return null;
|
|
}
|
|
color.a = targetA;
|
|
}
|
|
|
|
IEnumerator WaitVideoStandbyFadeIn()
|
|
{
|
|
standby = true;
|
|
currentPlayer.Play();
|
|
mat.mainTexture = currentPlayer.Texture;
|
|
yield return StartCoroutine(Fade(1));
|
|
}
|
|
|
|
IEnumerator WaitVideoStopTimeFadeOut()
|
|
{
|
|
yield return new WaitForSeconds(VideoStopTime);
|
|
yield return StartCoroutine(Fade(0));
|
|
currentPlayer.Pause();
|
|
currentPlayer.Frame = 0;
|
|
}
|
|
|
|
|
|
public bool isTriggerCollider;
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
isTriggerCollider = false;
|
|
}
|
|
|
|
void OnTriggerStay(Collider other)
|
|
{
|
|
isTriggerCollider = true;
|
|
}
|
|
|
|
private void OnTriggerEnter(Collider other)
|
|
{
|
|
isTriggerCollider = true;
|
|
}
|
|
|
|
|
|
public float GetDisAppear()
|
|
{
|
|
float distime = DisAppearTime - Random.Range(0, DisAppearTime / 2);
|
|
distime = DisAppearTime * distime - distime * distime + 0.5f * distime;
|
|
return distime;
|
|
}
|
|
|
|
IEnumerator RandomPosNoTriggerOther()
|
|
{
|
|
RandomAppearWall();
|
|
yield return null;
|
|
yield return new WaitForSeconds(0.1f);
|
|
while (isTriggerCollider)
|
|
{
|
|
RandomAppearWall();
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|
|
|