Skip to content

Commit

Permalink
2017.3 is broken (#5)
Browse files Browse the repository at this point in the history
* Abstract Vimeo sign into a class
* Display proper error when using invalid token
* Update readme to exclude unpublished asset in asset store
* Save token on a per scene basis
* Use scene name as a loose way to determine uniqueness for token saving
  • Loading branch information
caseypugh authored Jan 8, 2018
1 parent fb9f2ac commit 94e7f00
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 120 deletions.
50 changes: 50 additions & 0 deletions Assets/Vimeo/Scripts/Config/VimeoBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Vimeo.Config
{
public class VimeoBehavior : MonoBehaviour
{
public string vimeoToken;
public bool saveVimeoToken = true;
public bool vimeoSignIn = false;
private const string VIMEO_TOKEN_NAME = "vimeo-token-";

public string GetVimeoToken()
{
if (saveVimeoToken) {
return vimeoToken;
}
else {
return PlayerPrefs.GetString(VIMEO_TOKEN_NAME + this.gameObject.scene.name);
}
}

public void SetVimeoToken(string token)
{
// Wasn't able to DRY this up - PlayerPrefs started causing seg faults :/
string token_name = VIMEO_TOKEN_NAME + this.gameObject.scene.name;

if (saveVimeoToken) {
SetKey(token_name, null);
vimeoToken = token;
}
else {
vimeoToken = null;
SetKey(token_name, token);
}
}

public void SetKey(string key, string val)
{
if (val == null || val == "") {
PlayerPrefs.DeleteKey(key);
}
else {
PlayerPrefs.SetString(key, val);
}
PlayerPrefs.Save();
}
}
}
12 changes: 12 additions & 0 deletions Assets/Vimeo/Scripts/Config/VimeoBehavior.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 29 additions & 34 deletions Assets/Vimeo/Scripts/Config/VimeoConfig.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#if UNITY_EDITOR
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEditor;
using Vimeo;

Expand All @@ -17,28 +18,11 @@ public bool Authenticated(string token)
return token != "" && token != null;
}

public void DrawVimeoConfig(VimeoPlayer player)
{
var so = serializedObject;

if (Authenticated(player.GetVimeoToken()) && player.vimeoSignIn) {
EditorGUILayout.PropertyField(so.FindProperty("videoScreen"));
EditorGUILayout.PropertyField(so.FindProperty("audioSource"));
EditorGUILayout.PropertyField(so.FindProperty("vimeoVideoId"), new GUIContent("Vimeo Video URL"));
player.videoQualityIndex = EditorGUILayout.Popup("Video Quality", player.videoQualityIndex, player.videoQualities);

EditorGUILayout.Space();
}

DrawVimeoAuth(player);
so.ApplyModifiedProperties();
}

public void DrawVimeoAuth(VimeoPlayer player)
public void DrawVimeoAuth(VimeoBehavior auth)
{
var so = serializedObject;

if (!Authenticated(player.GetVimeoToken()) || !player.vimeoSignIn) {
if (!Authenticated(auth.GetVimeoToken()) || !auth.vimeoSignIn) {
EditorGUILayout.PropertyField(so.FindProperty("vimeoToken"));
EditorGUILayout.PropertyField(so.FindProperty("saveVimeoToken"), new GUIContent("Save token with scene"));

Expand All @@ -48,10 +32,10 @@ public void DrawVimeoAuth(VimeoPlayer player)
}

GUI.backgroundColor = Color.green;
if (Authenticated(player.vimeoToken)) {
if (Authenticated(auth.vimeoToken)) {
if (GUILayout.Button("Sign into Vimeo", GUILayout.Height(30))) {
(target as VimeoPlayer).SetVimeoToken(player.vimeoToken);
player.vimeoSignIn = true;
auth.SetVimeoToken(auth.vimeoToken);
auth.vimeoSignIn = true;
GUI.FocusControl(null);
}
}
Expand All @@ -62,27 +46,37 @@ public void DrawVimeoAuth(VimeoPlayer player)

GUI.backgroundColor = Color.red;
if (GUILayout.Button("Switch accounts")) {
player.vimeoSignIn = false;

if (target.GetType().ToString() == "Vimeo.VimeoPublisher") {
#if UNITY_2017_3_OR_NEWER
(target as VimeoPublisher).SetVimeoToken(null);
#endif
} else {
(target as VimeoPlayer).SetVimeoToken(null);
}
auth.vimeoSignIn = false;
auth.SetVimeoToken(null);
}
GUI.backgroundColor = Color.white;
}
}


public void DrawVimeoConfig(VimeoPlayer player)
{
var so = serializedObject;

if (Authenticated(player.GetVimeoToken()) && player.vimeoSignIn) {
EditorGUILayout.PropertyField(so.FindProperty("videoScreen"));
EditorGUILayout.PropertyField(so.FindProperty("audioSource"));
EditorGUILayout.PropertyField(so.FindProperty("vimeoVideoId"), new GUIContent("Vimeo Video URL"));
player.videoQualityIndex = EditorGUILayout.Popup("Video Quality", player.videoQualityIndex, player.videoQualities);

EditorGUILayout.Space();
}

DrawVimeoAuth(player);
so.ApplyModifiedProperties();
}

#if UNITY_2017_3_OR_NEWER
public void DrawVimeoConfig(VimeoPublisher publisher)
{
var so = serializedObject;
DrawVimeoAuth (publisher.GetVimeoToken());

if (Authenticated(publisher.GetVimeoToken())) {
if (Authenticated(publisher.GetVimeoToken()) && publisher.vimeoSignIn) {
EditorGUILayout.Space();

EditorGUILayout.PropertyField(so.FindProperty("_camera"));
Expand All @@ -100,6 +94,7 @@ public void DrawVimeoConfig(VimeoPublisher publisher)
DrawSlackConfig(publisher);
}

DrawVimeoAuth(publisher);
so.ApplyModifiedProperties();
}

Expand Down
7 changes: 6 additions & 1 deletion Assets/Vimeo/Scripts/Services/VimeoApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,12 @@ IEnumerator Request(string api_path)
yield return VimeoApi.SendRequest(request);

if (OnRequestComplete != null) {
OnRequestComplete(request.downloadHandler.text);
if (request.responseCode != 200) {
Debug.LogError(request.downloadHandler.text);
}
else {
OnRequestComplete(request.downloadHandler.text);
}
}
}
}
Expand Down
50 changes: 5 additions & 45 deletions Assets/Vimeo/Scripts/VimeoPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
using SimpleJSON;

using Vimeo.Services;
using Vimeo.Config;
using System.Text.RegularExpressions;

namespace Vimeo
{
#if UNITY_EDITOR
using Vimeo.Config;
using UnityEditor;
[CustomEditor (typeof(VimeoPlayer))]
public class VimeoPlayerInspector : VimeoConfig
Expand All @@ -34,15 +34,15 @@ private static void Create360Prefab() {

public override void OnInspectorGUI()
{
var publisher = target as VimeoPlayer;
DrawVimeoConfig(publisher);
var player = target as VimeoPlayer;
DrawVimeoConfig(player);
EditorUtility.SetDirty(target);
}
}
#endif

[AddComponentMenu("Video/Vimeo Player")]
public class VimeoPlayer : MonoBehaviour
public class VimeoPlayer : VimeoBehavior
{
public delegate void VimeoEvent();
public event VimeoEvent OnVideoStart;
Expand All @@ -51,9 +51,7 @@ public class VimeoPlayer : MonoBehaviour

public GameObject videoScreen;
public GameObject audioSource;
public string vimeoToken;
public bool saveVimeoToken = true;
public bool vimeoSignIn = false;

public string vimeoVideoId;

public string[] videoQualities = new [] { "Highest", "1080", "720", "540", "480", "360" };
Expand Down Expand Up @@ -107,44 +105,6 @@ private void Start()
}
}

public string GetVimeoToken()
{
if (saveVimeoToken) {
return vimeoToken;
}
else {
return PlayerPrefs.GetString("vimeo-player-token");
}
}

public void SetVimeoToken(string token)
{
if (saveVimeoToken) {
SetKey("vimeo-player-token", null);
vimeoToken = token;
}
else {
vimeoToken = null;
SetKey("vimeo-player-token", token);
}
}

private void SetKey(string key, string val)
{
if (val == null || val == "") {
PlayerPrefs.DeleteKey(key);
} else {
PlayerPrefs.SetString(key, val);
}
PlayerPrefs.Save();
}

private void OnDisable()
{
api.OnRequestComplete -= OnLoadVimeoVideoComplete;
controller.OnVideoStart -= VideoStarted;
}

public void LoadVimeoVideo(int id)
{
api.GetVideoFileUrlByVimeoId(id);
Expand Down
44 changes: 7 additions & 37 deletions Assets/Vimeo/Scripts/VimeoPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@

namespace Vimeo {

[CustomEditor (typeof(VimeoPublisher))]
[CustomEditor(typeof(VimeoPublisher))]
public class VimeoPublisherInspector : VimeoConfig
{
public override void OnInspectorGUI()
{
var player = target as VimeoPublisher;
DrawVimeoConfig (player);
var publisher = target as VimeoPublisher;
DrawVimeoConfig(publisher);
EditorUtility.SetDirty(target);
}
}

[AddComponentMenu("Vimeo/Video Recorder")]
public class VimeoPublisher : MonoBehaviour {

public class VimeoPublisher : VimeoBehavior {
public enum LinkType
{
VideoPage,
Expand All @@ -40,7 +40,6 @@ public enum LinkType

public VimeoApi.PrivacyMode m_privacyMode = VimeoApi.PrivacyMode.anybody;
public LinkType defaultShareLink = LinkType.VideoPage;
public string vimeoToken;

public bool recordOnStart = false;
public bool openInBrowser = false;
Expand All @@ -60,7 +59,7 @@ public enum LinkType

private Coroutine saveCoroutine;

void Start ()
void Start()
{
if (_camera == null) {
_camera = Camera.main;
Expand Down Expand Up @@ -90,25 +89,6 @@ void Start ()
}
}

public string GetVimeoToken()
{
var token = PlayerPrefs.GetString("vimeo-token");
if (token == null || token == "") {
if (vimeoToken != null && vimeoToken != "") {
SetVimeoToken (vimeoToken);
}
token = vimeoToken;
}

vimeoToken = null;
return token;
}

public void SetVimeoToken(string token)
{
SetKey("vimeo-token", token);
}

public string GetSlackToken()
{
var token = PlayerPrefs.GetString("slack-token");
Expand All @@ -128,16 +108,6 @@ public void SetSlackToken(string token)
SetKey("slack-token", token);
}

private void SetKey(string key, string val)
{
if (val == null || val == "") {
PlayerPrefs.DeleteKey(key);
} else {
PlayerPrefs.SetString(key, val);
}
PlayerPrefs.Save();
}

public void BeginRecording()
{
videoId = null;
Expand Down Expand Up @@ -294,7 +264,7 @@ void LateUpdate()
}
}
}

}
}

#endif
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ The SDK comes with the following example Unity scenes in the `Vimeo/Scenes` fold
</table>

### 1. Installation
There are two ways to install:
* Download the most recent `.unitypackage` from the [latest releases](https://github.com/vimeo/vimeo-unity-sdk/releases) and open it up.
* Or download from the [Unity Asset Store](http://u3d.as/127G)
Download the most recent `.unitypackage` from the [latest releases](https://github.com/vimeo/vimeo-unity-sdk/releases) and open it up.

### 2. Choose your playback method
Open up your Unity scene or create a new one. For streaming any Vimeo video, you will be using the `VimeoPlayer` component. There are two ways to setup the Vimeo player in Unity:
Expand Down

0 comments on commit 94e7f00

Please sign in to comment.