322 lines
6.1 KiB
C#
322 lines
6.1 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
[ExecuteInEditMode]
|
|
public class MegaBook : MonoBehaviour
|
|
{
|
|
public float dragsensi = 1f;
|
|
|
|
public float keysensi = 1f;
|
|
|
|
public GameObject front;
|
|
|
|
public GameObject back;
|
|
|
|
public GameObject page1;
|
|
|
|
public GameObject page2;
|
|
|
|
public GameObject page3;
|
|
|
|
public List<Texture> pages = new List<Texture>();
|
|
|
|
public float bookalpha;
|
|
|
|
public float covergap;
|
|
|
|
public float pagespace = 0.01f;
|
|
|
|
public bool interactive;
|
|
|
|
public bool useMouse;
|
|
|
|
public KeyCode prevPageKey = KeyCode.A;
|
|
|
|
public KeyCode nextPageKey = KeyCode.D;
|
|
|
|
private MegaPageFlip pf1;
|
|
|
|
private MegaPageFlip pf2;
|
|
|
|
private MegaPageFlip pf3;
|
|
|
|
private MeshRenderer mrpg1;
|
|
|
|
private MeshRenderer mrpg2;
|
|
|
|
private MeshRenderer mrpg3;
|
|
|
|
private int currentPage;
|
|
|
|
private bool pageTurning;
|
|
|
|
private MegaModifyObject pobj1;
|
|
|
|
private MegaModifyObject pobj2;
|
|
|
|
private MegaModifyObject pobj3;
|
|
|
|
private float page1turn = -1f;
|
|
|
|
private float page2turn = -1f;
|
|
|
|
private float page3turn = -1f;
|
|
|
|
private void SetPageTexture(MeshRenderer mr, int i, Texture t)
|
|
{
|
|
if (mr.sharedMaterials[i].mainTexture != t)
|
|
{
|
|
mr.sharedMaterials[i].mainTexture = t;
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (interactive)
|
|
{
|
|
Interactive();
|
|
}
|
|
if (page1 == null || page2 == null || page3 == null)
|
|
{
|
|
return;
|
|
}
|
|
if (page1 != null && pf1 == null)
|
|
{
|
|
pf1 = page1.GetComponent<MegaPageFlip>();
|
|
}
|
|
if (mrpg1 == null)
|
|
{
|
|
mrpg1 = page1.GetComponent<MeshRenderer>();
|
|
}
|
|
if (page2 != null && pf2 == null)
|
|
{
|
|
pf2 = page2.GetComponent<MegaPageFlip>();
|
|
}
|
|
if (mrpg2 == null)
|
|
{
|
|
mrpg2 = page2.GetComponent<MeshRenderer>();
|
|
}
|
|
if (page3 != null && pf3 == null)
|
|
{
|
|
pf3 = page3.GetComponent<MegaPageFlip>();
|
|
}
|
|
if (mrpg3 == null)
|
|
{
|
|
mrpg3 = page3.GetComponent<MeshRenderer>();
|
|
}
|
|
if (pf1 == null || pf2 == null || pf3 == null || front == null || back == null)
|
|
{
|
|
return;
|
|
}
|
|
if (pobj1 == null)
|
|
{
|
|
pobj1 = page1.GetComponent<MegaModifyObject>();
|
|
}
|
|
if (pobj2 == null)
|
|
{
|
|
pobj2 = page2.GetComponent<MegaModifyObject>();
|
|
}
|
|
if (pobj3 == null)
|
|
{
|
|
pobj3 = page3.GetComponent<MegaModifyObject>();
|
|
}
|
|
int num = pages.Count / 2 + 2;
|
|
if (bookalpha < 0f)
|
|
{
|
|
bookalpha = 0f;
|
|
}
|
|
if (bookalpha > 100f)
|
|
{
|
|
bookalpha = 100f;
|
|
}
|
|
if (front.transform.childCount > 0)
|
|
{
|
|
Transform child = front.transform.GetChild(0);
|
|
if (child != null)
|
|
{
|
|
Vector3 zero = Vector3.zero;
|
|
zero.y = covergap * 0.5f;
|
|
child.localPosition = zero;
|
|
}
|
|
}
|
|
if (back.transform.childCount > 0)
|
|
{
|
|
Transform child2 = back.transform.GetChild(0);
|
|
if (child2 != null)
|
|
{
|
|
Vector3 zero2 = Vector3.zero;
|
|
zero2.y = (0f - covergap) * 0.5f;
|
|
child2.localPosition = zero2;
|
|
}
|
|
}
|
|
double num2 = (double)bookalpha / 100.0;
|
|
int num3 = (int)((double)num * num2);
|
|
double num4 = 1.0 / (double)num;
|
|
double num5 = num2 % num4 / num4;
|
|
Vector3 zero3 = Vector3.zero;
|
|
if (num3 == 0)
|
|
{
|
|
zero3.z = 180f * (float)num5;
|
|
}
|
|
else
|
|
{
|
|
zero3.z = 180f;
|
|
}
|
|
front.transform.localRotation = Quaternion.Euler(zero3);
|
|
if (num3 >= num - 1)
|
|
{
|
|
zero3.z = 180f * (float)num5;
|
|
}
|
|
else
|
|
{
|
|
zero3.z = 0f;
|
|
}
|
|
back.transform.localRotation = Quaternion.Euler(zero3);
|
|
if (num >= 3)
|
|
{
|
|
if (num3 == 1)
|
|
{
|
|
pf1.turn = Mathf.Clamp((float)(num5 * 100.0), 0f, 100f);
|
|
pf2.turn = 0f;
|
|
pf3.turn = 0f;
|
|
}
|
|
else if (num3 == num - 2)
|
|
{
|
|
pf1.turn = 100f;
|
|
pf2.turn = 100f;
|
|
pf3.turn = Mathf.Clamp((float)(num5 * 100.0), 0f, 100f);
|
|
}
|
|
else if (num3 == 0)
|
|
{
|
|
pf1.turn = 0f;
|
|
pf2.turn = 0f;
|
|
pf3.turn = 0f;
|
|
}
|
|
else if (num3 >= num - 1)
|
|
{
|
|
pf1.turn = 100f;
|
|
pf2.turn = 100f;
|
|
pf3.turn = 100f;
|
|
}
|
|
else
|
|
{
|
|
pf1.turn = 100f;
|
|
pf2.turn = Mathf.Clamp((float)(num5 * 100.0), 0f, 100f);
|
|
pf3.turn = 0f;
|
|
}
|
|
Vector3 zero4 = Vector3.zero;
|
|
zero4.y = Mathf.Lerp(pagespace, 0f - pagespace, pf1.turn * 0.01f);
|
|
page1.transform.localPosition = zero4;
|
|
zero4.y = Mathf.Lerp(0f, 0f, pf2.turn * 0.01f);
|
|
page2.transform.localPosition = zero4;
|
|
zero4.y = Mathf.Lerp(0f - pagespace, pagespace, pf3.turn * 0.01f);
|
|
page3.transform.localPosition = zero4;
|
|
int num6 = num3 - 2;
|
|
if (num6 < 0)
|
|
{
|
|
num6 = 0;
|
|
}
|
|
num6 *= 2;
|
|
if (num6 < pages.Count - 1)
|
|
{
|
|
SetPageTexture(mrpg1, 0, pages[num6]);
|
|
SetPageTexture(mrpg1, 1, pages[num6 + 1]);
|
|
}
|
|
if (num6 < pages.Count - 3 && num6 < pages.Count - 5)
|
|
{
|
|
SetPageTexture(mrpg2, 0, pages[num6 + 2]);
|
|
SetPageTexture(mrpg2, 1, pages[num6 + 3]);
|
|
}
|
|
if (num6 < pages.Count - 5)
|
|
{
|
|
SetPageTexture(mrpg3, 0, pages[num6 + 4]);
|
|
SetPageTexture(mrpg3, 1, pages[num6 + 5]);
|
|
}
|
|
if (pf1.turn != page1turn)
|
|
{
|
|
page1turn = pf1.turn;
|
|
pobj1.Enabled = true;
|
|
}
|
|
else if (page1turn == 100f || page1turn == 0f)
|
|
{
|
|
pobj1.Enabled = false;
|
|
}
|
|
else
|
|
{
|
|
pobj1.Enabled = true;
|
|
}
|
|
if (pf2.turn != page2turn)
|
|
{
|
|
page2turn = pf2.turn;
|
|
pobj2.Enabled = true;
|
|
}
|
|
else if (page2turn == 100f || page2turn == 0f)
|
|
{
|
|
pobj2.Enabled = false;
|
|
}
|
|
else
|
|
{
|
|
pobj2.Enabled = true;
|
|
}
|
|
if (pf3.turn != page3turn)
|
|
{
|
|
page3turn = pf3.turn;
|
|
pobj3.Enabled = true;
|
|
}
|
|
else if (page3turn == 100f || page3turn == 0f)
|
|
{
|
|
pobj3.Enabled = false;
|
|
}
|
|
else
|
|
{
|
|
pobj3.Enabled = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Interactive()
|
|
{
|
|
if (pageTurning)
|
|
{
|
|
return;
|
|
}
|
|
if (useMouse)
|
|
{
|
|
bookalpha += Input.GetAxis("Mouse X") * dragsensi;
|
|
bookalpha = Mathf.Clamp(bookalpha, 0f, 100f);
|
|
return;
|
|
}
|
|
if (Input.GetKeyDown(prevPageKey))
|
|
{
|
|
StartCoroutine(FlipToPage(currentPage - 1));
|
|
}
|
|
if (Input.GetKeyDown(nextPageKey))
|
|
{
|
|
StartCoroutine(FlipToPage(currentPage + 1));
|
|
}
|
|
}
|
|
|
|
private IEnumerator FlipToPage(int page)
|
|
{
|
|
pageTurning = true;
|
|
double totalPageCount = (pages.Count + 4) / 2;
|
|
double target = (double)page / totalPageCount * 99.999;
|
|
if (target < 0.0)
|
|
{
|
|
target = 0.0;
|
|
}
|
|
if (target > 100.0)
|
|
{
|
|
target = 100.0;
|
|
}
|
|
while (bookalpha != (float)target)
|
|
{
|
|
bookalpha = Mathf.MoveTowards(bookalpha, (float)target, keysensi * Time.deltaTime);
|
|
yield return 0;
|
|
}
|
|
currentPage = page;
|
|
pageTurning = false;
|
|
}
|
|
}
|