using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
///
/// 端口线条信息
///
[Serializable]
public class LineInfor : MonoBehaviour
{
//InputField t;
[HideInInspector] public Root root;
public List lines = new List();
public string cableGroupName = "";
public string cableName = "";
public string portType = "";
LineRenderer lineRenderer;
public float speed = 1f; // 线流动的速度
private Material material;
private Vector2 tiling;
private Vector2 offset;
private int mainTexProperty;
// 线长
[SerializeField] private float lineLen;
// 密度
[SerializeField]
private float density = 2f;
// 定时器
[SerializeField] private float timer = 0;
// 频率间隔
[SerializeField]
private float frequency = 0.03f;
//爬行速度
[SerializeField]
private float moveSpeed = 0.04f;
private void Awake()
{
lineRenderer = GetComponent();
//if (PlayerPrefs.HasKey(this.root.myID))
//{
// string json = PlayerPrefs.GetString(root.myID);
// Root myObject = JsonUtility.FromJson(json);
// // 使用加载的字段值
// this.root.otherID = myObject.otherID;
//}
}
private void Start()
{
//// 缓存材质实例
//material = lineRenderer.material;
//// 缓存属性id,防止下面设置属性的时候重复计算名字的哈希
//mainTexProperty = Shader.PropertyToID("_MainTex");
//tiling = new Vector2(20, 0);
//offset = new Vector2(0, 0);
//// 设置Tiling
//material.SetTextureScale(mainTexProperty, tiling);
//// 设置Offset
//material.SetTextureOffset(mainTexProperty, offset);
//lineLen = (lineRenderer.GetPosition(1) - lineRenderer.GetPosition(0)).magnitude;
//// 根据线段长度计算Tiling
//tiling = new Vector2(lineLen * density, 0);
//// 设置Tiling
//material.SetTextureScale(mainTexProperty, tiling);
}
private void Update()
{
//if (portType == "电缆线")
//{
// run_line();
//}
//if (!string.IsNullOrEmpty(root.otherID)) return;
//root.otherID = t.text;
//if (string.IsNullOrEmpty(root.otherID)) return;
//root.otherID = t.text;
//string json = JsonConvert.SerializeObject(root);
//PlayerPrefs.SetString(root.myID, json);
//PlayerPrefs.Save();
if (GameManager.Inst.magnifyState)
{
if (lineRenderer.startWidth == lineWidth_min)
return;
setLineWidth(lineWidth_min);
}
else
{
if (lineRenderer.startWidth == lineWidth_max)
return;
setLineWidth(lineWidth_max);
}
}
public void run_line()
{
// 计算线长度
lineLen = (lineRenderer.GetPosition(1) - lineRenderer.GetPosition(0)).magnitude;
// 根据线段长度计算Tiling
tiling = new Vector2(lineLen * density, 0);
// 设置Tiling
material.SetTextureScale(mainTexProperty, tiling);
timer += Time.deltaTime;
if (timer >= frequency)
{
timer = 0;
offset -= new Vector2(moveSpeed, 0);
material.SetTextureOffset(mainTexProperty, offset);
}
}
[ContextMenu("查看PlayerPrefs")]
void PrintAllPlayerPrefs()
{
// 获取PlayerPrefs中所有的键
string[] keys = GetAllPlayerPrefsKeys();
// 遍历所有的键
foreach (string key in keys)
{
string value = PlayerPrefs.GetString(key);
Debug.Log("Key: " + key + ", Value: " + value);
}
}
// 获取PlayerPrefs中所有的键
private string[] GetAllPlayerPrefsKeys()
{
int count = PlayerPrefs.GetInt("PlayerPrefsCount");
string[] keys = new string[count];
for (int i = 0; i < count; i++)
{
keys[i] = PlayerPrefs.GetString("PlayerPrefsKey" + i.ToString());
}
return keys;
}
public float lineWidth_min = 0.01f;
public float lineWidth_max = 0.1f;
///
/// 设置LineRenderer宽度
///
///
[ContextMenu("设置LineRenderer宽度")]
public void setLineWidth(float lineWidth)
{
lineRenderer.startWidth = lineWidth;
lineRenderer.endWidth = lineWidth;
}
#region JSON
[System.Serializable]
public class Root
{
[Header("自己ID")] public string myID;
[Header("对方ID")] public string otherID;
[Header("线条UID")] public string lineUID;
}
#endregion
}