123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- using wf;
- public abstract class BasePhotoCustomObject : MonoBehaviour, IComparable<BasePhotoCustomObject>
- {
- public static BasePhotoCustomObject CreatePrefab(GameObject parent, BasePhotoCustomObject.Type createType)
- {
- if (createType == BasePhotoCustomObject.Type.Plane)
- {
- GameObject gameObject = Utility.CreatePrefab(parent, "ScenePhotoMode/CustomObject/Plane", false);
- return gameObject.GetComponent<BasePhotoCustomObject>();
- }
- if (createType == BasePhotoCustomObject.Type.Sphere)
- {
- GameObject gameObject2 = Utility.CreatePrefab(parent, "ScenePhotoMode/CustomObject/Sphere", false);
- return gameObject2.GetComponent<BasePhotoCustomObject>();
- }
- return null;
- }
- public static BasePhotoCustomObject InstantiateFromFile(GameObject parent, string fileFullPath)
- {
- KeyValuePair<bool, BasePhotoCustomObject.Type> typeFromFile = BasePhotoCustomObject.GetTypeFromFile(fileFullPath);
- if (!typeFromFile.Key)
- {
- return null;
- }
- BasePhotoCustomObject.Type value = typeFromFile.Value;
- BasePhotoCustomObject basePhotoCustomObject = BasePhotoCustomObject.CreatePrefab(parent, value);
- basePhotoCustomObject.Desilialize(File.ReadAllBytes(fileFullPath));
- return basePhotoCustomObject;
- }
- public static KeyValuePair<bool, BasePhotoCustomObject.Type> GetTypeFromFile(string fileFullPath)
- {
- bool key = false;
- if (string.IsNullOrEmpty(fileFullPath) || !File.Exists(fileFullPath))
- {
- return new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, BasePhotoCustomObject.Type.Plane);
- }
- KeyValuePair<bool, BasePhotoCustomObject.Type> result;
- using (FileStream fileStream = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read))
- {
- using (BinaryReader binaryReader = new BinaryReader(fileStream))
- {
- string a = binaryReader.ReadString();
- if (a != "COM3D2_PHOTO_CUSTOM_OBJECT")
- {
- result = new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, BasePhotoCustomObject.Type.Plane);
- }
- else
- {
- int num = binaryReader.ReadInt32();
- BasePhotoCustomObject.Type value = (BasePhotoCustomObject.Type)Enum.Parse(typeof(BasePhotoCustomObject.Type), binaryReader.ReadString());
- key = true;
- result = new KeyValuePair<bool, BasePhotoCustomObject.Type>(key, value);
- }
- }
- }
- return result;
- }
- public abstract byte[] imageBinarys { get; }
- public abstract BasePhotoCustomObject.Type type { get; }
- public abstract Texture2D mainTexture { get; }
- public float scale
- {
- get
- {
- return this.scale_;
- }
- set
- {
- this.scale_ = value;
- this.SetScale(this.scale_);
- }
- }
- public bool enabledTextureColor
- {
- get
- {
- return this.enabledTextureColor_;
- }
- set
- {
- this.enabledTextureColor_ = value;
- this.SetTextureColor((!value) ? Color.white : this.textureColor);
- }
- }
- public Color textureColor
- {
- get
- {
- return this.textureColor_;
- }
- set
- {
- bool flag = this.textureColor_ != value && this.enabledTextureColor;
- this.textureColor_ = value;
- if (flag)
- {
- this.enabledTextureColor = true;
- }
- }
- }
- public bool visible
- {
- get
- {
- return base.gameObject.activeSelf;
- }
- set
- {
- base.gameObject.SetActive(value);
- }
- }
- public virtual void Awake()
- {
- this.Clear();
- }
- public virtual void OnDestroy()
- {
- }
- public bool SetTexture(string imageFilePath)
- {
- if (string.IsNullOrEmpty(imageFilePath))
- {
- return false;
- }
- if (!File.Exists(imageFilePath))
- {
- return false;
- }
- byte[] array = File.ReadAllBytes(imageFilePath);
- return array != null && array.Length != 0 && this.SetTexture(array);
- }
- public bool SetTexture(byte[] imageFileBinary)
- {
- return imageFileBinary != null && imageFileBinary.Length > 0 && this.CreateTexture2D(imageFileBinary);
- }
- public virtual void Clear()
- {
- this.scale = 1f;
- this.enabledTextureColor = true;
- this.textureColor = Color.white;
- }
- public virtual int CompareTo(BasePhotoCustomObject comp_obj)
- {
- return ((int)comp_obj.type).CompareTo((int)this.type);
- }
- public byte[] Serialize()
- {
- byte[] result = null;
- NDebug.Assert(this.mainTexture != null, "BasePhotoCustomObject::Serialize error.\nmainTextureがnullです");
- using (MemoryStream memoryStream = new MemoryStream())
- {
- using (BinaryWriter binaryWriter = new BinaryWriter(memoryStream))
- {
- binaryWriter.Write("COM3D2_PHOTO_CUSTOM_OBJECT");
- binaryWriter.Write(1270);
- binaryWriter.Write(this.type.ToString());
- binaryWriter.Write(this.scale_);
- binaryWriter.Write(this.enabledTextureColor_);
- binaryWriter.Write(this.textureColor_.a);
- binaryWriter.Write(this.textureColor_.r);
- binaryWriter.Write(this.textureColor_.g);
- binaryWriter.Write(this.textureColor_.b);
- this.OnSerialize(binaryWriter);
- result = memoryStream.ToArray();
- }
- }
- return result;
- }
- public bool Desilialize(byte[] binary)
- {
- this.Clear();
- using (MemoryStream memoryStream = new MemoryStream(binary))
- {
- using (BinaryReader binaryReader = new BinaryReader(memoryStream))
- {
- string a = binaryReader.ReadString();
- NDebug.Assert(a == "COM3D2_PHOTO_CUSTOM_OBJECT", "BasePhotoCustomObject::Desilialize error.\nヘッダーが不正です");
- int gameVersion = binaryReader.ReadInt32();
- BasePhotoCustomObject.Type type = (BasePhotoCustomObject.Type)Enum.Parse(typeof(BasePhotoCustomObject.Type), binaryReader.ReadString());
- this.scale = binaryReader.ReadSingle();
- this.enabledTextureColor = binaryReader.ReadBoolean();
- this.textureColor = new Color
- {
- a = binaryReader.ReadSingle(),
- r = binaryReader.ReadSingle(),
- g = binaryReader.ReadSingle(),
- b = binaryReader.ReadSingle()
- };
- this.OnDesilialize(gameVersion, type, binaryReader);
- }
- }
- return true;
- }
- protected abstract void OnSerialize(BinaryWriter writer);
- protected abstract bool OnDesilialize(int gameVersion, BasePhotoCustomObject.Type type, BinaryReader reader);
- protected abstract bool CreateTexture2D(byte[] imageFileBinary);
- protected abstract void SetTextureColor(Color setColor);
- protected abstract void SetScale(float scale);
- protected float scale_;
- protected bool enabledTextureColor_;
- protected Color textureColor_;
- public enum Type
- {
- Plane,
- Sphere
- }
- }
|