public override object Revert(BinaryReader reader) { var type = reader.ReadByte(); if (type == 0) { return(DeserializeUnityAsset(typeof(Sprite), reader)); } if (type == 1) { Texture2D texture = null; var rect = default(Rect); var pivot = default(Vector2); var pixelsPerUnit = 100f; var border = default(Vector4); Deserialize(reader, ret => texture = (Texture2D)ret); Deserialize(reader, ret => rect = (Rect)ret); Deserialize(reader, ret => pivot = (Vector2)ret); pixelsPerUnit = reader.ReadSingle(); Deserialize(reader, ret => border = (Vector4)ret); return(Sprite.Create(texture, rect, pivot, pixelsPerUnit, 0, SpriteMeshType.Tight, border)); } throw new NotSupportedException("Invalid type: " + type); }
public void ConvertSpriteRenderer_ConvertLocalBounds_SameCenter(Vector2 spritePivot) { var tmpTexture = new Texture2D(16, 16); var sprite = Sprite.Create(tmpTexture, new Rect(Vector2.zero, Vector2.one), spritePivot, 1f); Vector2 pivotInWorldSpace; { Root = new GameObject(); var spriteRenderer = CreateClassicComponent <SpriteRenderer>(Root); spriteRenderer.sprite = sprite; var uWorldToLocalMatrix = spriteRenderer.transform.worldToLocalMatrix; pivotInWorldSpace = uWorldToLocalMatrix.MultiplyPoint(spriteRenderer.bounds.center); } Assert.DoesNotThrow(() => { RunConversion(Root); }); using (var query = EntityManager.CreateEntityQuery(typeof(Unity.Tiny.Renderer2D))) using (var renderers = query.ToComponentDataArray <Unity.Tiny.Renderer2D>(Allocator.TempJob)) { var objectBound = renderers[0].Bounds; var pivot = objectBound.Center; Assert.That(pivot.x, Is.EqualTo(pivotInWorldSpace.x).Within(EditorTestUtilities.Epsilon)); Assert.That(pivot.y, Is.EqualTo(pivotInWorldSpace.y).Within(EditorTestUtilities.Epsilon)); } Object.DestroyImmediate(sprite); Object.DestroyImmediate(tmpTexture); }
public void ConvertSpriteRenderer_ConvertBoundsWithLocalScaleAndSpriteSize_SameExtents(System.Tuple <Vector2, Vector2> localScaleAndSpriteSize) { var localScale = localScaleAndSpriteSize.Item1; var spriteSize = localScaleAndSpriteSize.Item2; var tmpTexture = new Texture2D(16, 16); var tmpSprite = Sprite.Create(tmpTexture, new Rect(Vector2.zero, spriteSize), Vector2.zero, 1f); { Root = new GameObject(); var spriteRenderer = CreateClassicComponent <SpriteRenderer>(Root); spriteRenderer.sprite = tmpSprite; spriteRenderer.transform.localScale = localScale; } Assert.DoesNotThrow(() => { RunConversion(Root); }); using (var query = EntityManager.CreateEntityQuery(typeof(Unity.Tiny.Renderer2D))) using (var renderers = query.ToComponentDataArray <Unity.Tiny.Renderer2D>(Allocator.TempJob)) { var objectBound = renderers[0].Bounds; var extents = objectBound.Extents; var boundExtentsX = localScale.x * spriteSize.x * 0.5f; var boundExtentsY = localScale.y * spriteSize.y * 0.5f; Assert.That(extents.x, Is.EqualTo(boundExtentsX).Within(EditorTestUtilities.Epsilon)); Assert.That(extents.y, Is.EqualTo(boundExtentsY).Within(EditorTestUtilities.Epsilon)); } Object.DestroyImmediate(tmpSprite); Object.DestroyImmediate(tmpTexture); }
protected virtual IEnumerator Start() { // Load level if (CytoidApplication.CurrentLevel != null) { Level = CytoidApplication.CurrentLevel; } else { Level = JsonConvert.DeserializeObject <Level>( File.ReadAllText(Application.persistentDataPath + "/player/level.json")); Level.BasePath = Application.persistentDataPath + "/player/"; CytoidApplication.CurrentChartType = Level.charts[0].type; var www = new WWW("file://" + Level.BasePath + Level.background.path); yield return(www); yield return(null); // Wait an extra frame www.LoadImageIntoTexture(CytoidApplication.BackgroundTexture); var backgroundSprite = Sprite.Create(CytoidApplication.BackgroundTexture, new Rect(0, 0, CytoidApplication.BackgroundTexture.width, CytoidApplication.BackgroundTexture.height), new Vector2(0, 0)); var background = GameObject.FindGameObjectWithTag("Background"); if (background != null) { background.GetComponent <Image>().sprite = backgroundSprite; // Fill the screen by adapting to the aspect ratio background.GetComponent <AspectRatioFitter>().aspectRatio = (float)CytoidApplication.BackgroundTexture.width / CytoidApplication.BackgroundTexture.height; yield return(null); // Wait an extra frame } www.Dispose(); Resources.UnloadUnusedAssets(); CytoidApplication.CurrentLevel = Level; } // System settings CytoidApplication.SetAutoRotation(false); if (Application.platform == RuntimePlatform.Android && !Level.IsInternal) { print("Using Android Native Audio"); GameOptions.Instance.UseAndroidNativeAudio = true; } // Load chart print("Loading chart"); if (CytoidApplication.CurrentChartType == null) { CytoidApplication.CurrentChartType = Level.charts[0].type; } var chartSection = Level.charts.Find(it => it.type == CytoidApplication.CurrentChartType); string chartText; if (Application.platform == RuntimePlatform.Android && Level.IsInternal) { var chartWww = new WWW(Level.BasePath + chartSection.path); yield return(chartWww); chartText = Encoding.UTF8.GetString(chartWww.bytes); } else { chartText = File.ReadAllText(Level.BasePath + chartSection.path, Encoding.UTF8); } Chart = new Chart( chartText, 0.8f + (5 - (int)PlayerPrefs.GetFloat("horizontal margin", 3) - 1) * 0.025f, (5.5f + (5 - (int)PlayerPrefs.GetFloat("vertical margin", 3)) * 0.5f) / 9.0f ); // Load audio print("Loading audio"); var audioPath = Level.BasePath + Level.GetMusicPath(CytoidApplication.CurrentChartType); if (GameOptions.Instance.UseAndroidNativeAudio) { NativeAudioId = ANAMusic.load(audioPath, true); Length = ANAMusic.getDuration(NativeAudioId); } else { var www = new WWW( (Level.IsInternal && Application.platform == RuntimePlatform.Android ? "" : "file://") + audioPath); yield return(www); AudioSource.clip = www.GetAudioClip(); Length = AudioSource.clip.length; www.Dispose(); } // Game options var options = GameOptions.Instance; options.HitboxMultiplier = PlayerPrefsExt.GetBool("larger_hitboxes") ? 1.5555f : 1.3333f; options.ShowEarlyLateIndicator = PlayerPrefsExt.GetBool("early_late_indicator"); options.ChartOffset = PlayerPrefs.GetFloat("main offset", 0); options.ChartOffset += ZPlayerPrefs.GetFloat(PreferenceKeys.ChartRelativeOffset(Level.id), 0); if (Application.platform != RuntimePlatform.Android && Headset.Detect()) { options.ChartOffset += ZPlayerPrefs.GetFloat("headset offset"); } if (CytoidApplication.CurrentHitSound.Name != "None") { #if (UNITY_IOS || UNITY_ANDROID) && !UNITY_EDITOR options.HitSound = NativeAudio.Load("Hits/" + CytoidApplication.CurrentHitSound.Name + ".wav"); #endif } Play.Init(Chart); print("Chart checksum: " + Chart.Checksum); // Rank data if (Play.IsRanked) { RankedPlayData = new RankedPlayData(); RankedPlayData.user = OnlinePlayer.Name; RankedPlayData.password = OnlinePlayer.Password; RankedPlayData.start = TimeExt.Millis(); RankedPlayData.id = Level.id; RankedPlayData.type = CytoidApplication.CurrentChartType; RankedPlayData.mods = string.Join(",", Array.ConvertAll(Play.Mods.ToArray(), mod => mod.ToString())); RankedPlayData.version = Level.version; RankedPlayData.chart_checksum = Chart.Checksum; RankedPlayData.device.width = Screen.width; RankedPlayData.device.height = Screen.height; RankedPlayData.device.dpi = (int)Screen.dpi; RankedPlayData.device.model = SystemInfo.deviceModel; CytoidApplication.CurrentRankedPlayData = RankedPlayData; CytoidApplication.CurrentUnrankedPlayData = null; } else { UnrankedPlayData = new UnrankedPlayData(); UnrankedPlayData.user = OnlinePlayer.Authenticated ? OnlinePlayer.Name : "local"; UnrankedPlayData.password = OnlinePlayer.Authenticated ? OnlinePlayer.Password : ""; UnrankedPlayData.id = Level.id; UnrankedPlayData.type = CytoidApplication.CurrentChartType; UnrankedPlayData.version = Level.version; UnrankedPlayData.chart_checksum = Chart.Checksum; CytoidApplication.CurrentRankedPlayData = null; CytoidApplication.CurrentUnrankedPlayData = UnrankedPlayData; } // Touch handlers if (!Mod.Auto.IsEnabled()) { LeanTouch.OnFingerDown += OnFingerDown; LeanTouch.OnFingerSet += OnFingerSet; LeanTouch.OnFingerUp += OnFingerUp; } yield return(new WaitForSeconds(0.2f)); View.OnStart(); IsLoaded = true; EventKit.Broadcast("game loaded"); // Wait for Storyboard while (!StoryboardController.Instance.Loaded) { yield return(null); } yield return(new WaitForSeconds(0.3f)); StartGame(); }
public override void SetTexture(Texture2D texture) { base.SetTexture(texture); image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero); }
public static Sprite ToSprite(this Texture2D texture) { return(Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f))); }